Results 1 to 10 of 10

Thread: C# Problem

  1. #1

    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!

  2. #2
    * snip *
    Last edited by Tin_Tin; 11-06-2009 at 05:04 PM.

  3. #3
    Elite Member
    Join Date
    Aug 2008
    Posts
    1,053
    Blog Entries
    1
    can you upload the format of the text file?

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

    foo
    bazz
    bar

  5. #5
    erhmmm put a break point and check your strings! for extra characters.

  6. #6
    Elite Member
    Join Date
    Aug 2008
    Posts
    1,053
    Blog Entries
    1
    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:

    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.

    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

  7. #7
    you can use streamreader.readline().

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

    simple sample lang. hehehe

  8. #8
    maayuha jud ani ni artoy oi heheh

  9. #9
    Elite Member
    Join Date
    Aug 2008
    Posts
    1,053
    Blog Entries
    1
    Tin_Tin how was it?

  10. #10
    You tried this?

    if (names.Contains("foo"))
    {
    //Do your stuff here if true.
    }
    else
    {
    //Do your stuff here if false.
    }

  11.    Advertisement

Similar Threads

 
  1. SGPT Problem
    By Ferl in forum Fitness & Health
    Replies: 78
    Last Post: 03-25-2013, 11:44 AM
  2. Problem with your Symbian Cellphone Software?
    By Seņor Joze in forum Gizmos & Gadgets (Old)
    Replies: 421
    Last Post: 08-28-2010, 05:15 PM
  3. Replies: 221
    Last Post: 06-02-2009, 01:22 AM
  4. Playstation 2 Problem
    By baldog in forum Gizmos & Gadgets (Old)
    Replies: 18
    Last Post: 03-02-2006, 08:28 AM
  5. m125 problem
    By eliel73173 in forum Gizmos & Gadgets (Old)
    Replies: 6
    Last Post: 04-29-2005, 01:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
about us
We are the first Cebu Online Media.

iSTORYA.NET is Cebu's Biggest, Southern Philippines' Most Active, and the Philippines' Strongest Online Community!
follow us
#top