iSTORYA.NET

Go Back   iSTORYA.NET > Science & Technology > Software & Games > Programming
: :

Register FAQ Members List Calendar Mark Forums Read

Programming :: Programming related discussions ::

Reply
LinkBack Thread Tools
  #1  
Old 11-06-2009, 04:44 PM
C.I.A.
Tin_Tin is offline
Tin_Tin's Avatar
Join Date: Nov 2003
Gender: Male
Posts: 2,913
Default C# Problem

Code:
using System;
using System.IO;

class TestSearch
{
	public static void Main()
	{
		StreamReader streamReader = new StreamReader("search.txt");
		string text = streamReader.ReadToEnd();
		streamReader.Close();
		string[] names = text.Split('\n');
		
		if(MatchString(names, "foo")
			Console.Write("match");
		else
			Console.Write("no match");
	}

	public static bool MatchString(string[] data, string value)
	{
		foreach (string i in data)
		{
 			if (String.Compare(i,value)==0)
			{
				return true;
			}
		}
		return false;
	}
}
This code would take extract the data contained in the text file (search.txt) and place it inside an array. The search.txt file contains foo, bazz and bar. When I use the MatchString function I created I always get a "no match" even if the value I passed on to the function is inside the file. Please, I need your help mga masters!

If you have a better way of doing this please post it!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2  
Old 11-06-2009, 04:55 PM
C.I.A.
Tin_Tin is offline
Tin_Tin's Avatar
Join Date: Nov 2003
Gender: Male
Posts: 2,913
Default

* snip *
Last edited by Tin_Tin; 11-06-2009 at 05:04 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3  
Old 11-06-2009, 06:29 PM
Senior Member
MarkCuering is offline
Join Date: Aug 2008
Posts: 603
Blog Entries: 1
Default

can you upload the format of the text file?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4  
Old 11-06-2009, 08:46 PM
C.I.A.
Tin_Tin is offline
Tin_Tin's Avatar
Join Date: Nov 2003
Gender: Male
Posts: 2,913
Default

Its just plain text separated by carriage return (\n) like this:

foo
bazz
bar
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5  
Old 11-07-2009, 08:10 AM
Elite Member
silent-kill is online now
silent-kill's Avatar
Join Date: Mar 2003
Gender: Male
Posts: 1,742
Default

erhmmm put a break point and check your strings! for extra characters.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6  
Old 11-07-2009, 08:36 AM
Senior Member
MarkCuering is offline
Join Date: Aug 2008
Posts: 603
Blog Entries: 1
Default

Actually, I’d like to know where is this file came from, and what does this file provides.

streamReader.ReadToEnd works on unicode strings, CR (carriage return) or \r is different to LF (new line) \n. This is a long story of Unix and Windows of reading/writing files. Some application changes this automatically or ignore, depending on the system and in programming it depends also how the method provide you the solution.

Anyway, you can use:

Quote:
string[] names = Regex.Split(text, "\r\n");
string[] names = text.Split('\r');
Please take note also in comparing strings in .NET, they eventually compare it first by length of your string, which very effective when it comes to large searching and matching. Try to evaluate your code by under this on for loop.

Quote:
Console.WriteLine("{0}, {1}", i.ToString().Length, value.Length);

IMHO, you should practice using BufferedStream especially if you are soon to read larger than 4096 bytes (not pretty sure on this) and combining it with NetworkStream, they work both smoothly.


Have fun… GoodLuck. I'm enjoying myself studying on this thing :-d
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7  
Old 11-09-2009, 10:18 AM
Junior Member
Artoy is offline
Artoy's Avatar
Join Date: Sep 2008
Gender: Male
Posts: 65
Default

you can use streamreader.readline().

while(!streamReader.EndOfStream)
{
string text = streamReader.ReadLine();
//do comaprison here. like text == "foo"
}

simple sample lang. hehehe
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8  
Old Yesterday, 08:03 PM
Senior Member
menatsu is offline
menatsu's Avatar
Join Date: Aug 2006
Gender: Male
Posts: 504
Default

maayuha jud ani ni artoy oi heheh
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9  
Old Today, 02:25 PM
Senior Member
MarkCuering is offline
Join Date: Aug 2008
Posts: 603
Blog Entries: 1
Default

Tin_Tin how was it?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Very wierd.. problem. Not really a problem but very annoying Lord_Ac Software & Games 13 12-18-2008 01:11 PM
Need help with this problem daddyfree Computer Hardware 14 10-01-2008 08:49 AM
Weird CS 1.6 Problem ( Problem Solved ) IT Software & Games 0 01-03-2008 06:31 AM
I have a problem trlated problem with affiliate program, pls help *dudes* Websites & Multimedia 3 08-16-2006 08:57 AM
DVD problem Tin_Tin Computer Hardware 6 04-05-2006 08:24 AM


All times are GMT +8. The time now is 10:13 PM.


Powered by vBulletin® Version 3.7.6
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0
(c) 2002-2009 iSTORYA.NET | Design by DrE | Modifications by BeoR