Thread: String Comparison Issues

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    55

    String Comparison Issues

    If I have the following test code:

    Code:
    String content = "This is a test";
    String str = "This";
    String lookFor = content.Substring(0, 5); // "This"
    Boolean found = false;
    
    // "found" should equal true, BUT IT'S NOT!!!  Why??
    if (str == lookFor)
    	found = true;
    
    // "found" should equal true, BUT IT'S NOT!!!  Why??
    if (str.Equals(lookFor))
    	found = true;
    	
    // "found" should equal true, BUT IT'S NOT!!!  Why??
    switch (lookFor)
    {
    	case "This":
    		found = true;
    		break;
    }
    In all three examples, "found" should be true, however, it never happens.

    Where am I going wrong?

    Thanks.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > String lookFor = content.Substring(0, 5); // "This"
    You ask for 5 chars, and so you have a trailing space (I guess)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    41
    Quote Originally Posted by Salem View Post
    > String lookFor = content.Substring(0, 5); // "This"
    You ask for 5 chars, and so you have a trailing space (I guess)
    That is your problem. You are comparing "This" with "This ". Never going to be equal.

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    55
    Yes, correct. And when I do Substring(0, 4), everything works great. My mistake totally. I suppose my "example" works fine, but here's my 'real' code that puzzles me.

    When I run the following code:

    Code:
    String needle = "XGCa:004";
    String haystack = "\x1DXGCa:004\r\n845123\r\n";
    String search = haystack.Substring(0, 9);
    The "search" string shows as "XGCa:004"

    So now when I do:
    Code:
    Boolean found = false;
    
    if (search.Equals(needle))
        found = true;
    "found" never equals true.

    For some reason that I'm not understanding, the "\x1D" ASCII control sequence that I'm sending first is throwing things all off. EVEN THOUGH the "search" string distinctly shows "XGCa:004". Even if I .trim() everything, "found" never gets true.

    Any ideas?

    Thank you, and my apologies for not being precise on my first post.
    Last edited by xwielder; 05-16-2012 at 03:49 PM. Reason: left out one small thing

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > String search = haystack.Substring(0, 9);
    How about
    String search = haystack.Substring(1,8);

    > if (search.Equals(needle))
    Because you're trying to compare 9 chars with 8 (again!)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Nov 2006
    Posts
    55

    Thumbs up *solved*

    Quote Originally Posted by Salem View Post
    > String search = haystack.Substring(0, 9);
    How about
    String search = haystack.Substring(1,8);

    > if (search.Equals(needle))
    Because you're trying to compare 9 chars with 8 (again!)
    Thank you. (1,8) works perfect.

    Only reason I did (0,9) is because if I did (0,8), I would get "XGCa:00"

    Thank you, Salem, for pointing out (1,8). This has resolved my issue.

  7. #7
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    Since you don't appear to need the needle's starting index or anything else other than a bool to confirm the needle's existance in the haystack, you could use .Contains instead...

    Code:
    String needle = "XGCa:004";
    String haystack = "\x1DXGCa:004\r\n845123\r\n";
    bool found = haystack.Contains(needle);
    If you wish to insist on using Substring, you could avoid mistakes by offering the needle's character length as the substring's second parameter...

    Code:
    String needle = "XGCa:004";
    String haystack = "\x1DXGCa:004\r\n845123\r\n";
    bool found = haystack.Substring(1, needle.Length) == needle;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Comparison
    By Darkobra in forum C Programming
    Replies: 7
    Last Post: 01-06-2012, 04:56 PM
  2. std::string comparison versus int comparison
    By leeor_net in forum C++ Programming
    Replies: 3
    Last Post: 04-12-2009, 07:28 AM
  3. string comparison
    By sj999 in forum C Programming
    Replies: 4
    Last Post: 05-23-2008, 03:10 AM
  4. please help with a string comparison.
    By MegaManZZ in forum C Programming
    Replies: 5
    Last Post: 02-18-2008, 01:33 PM
  5. String Comparison
    By Cdrwolfe in forum C++ Programming
    Replies: 8
    Last Post: 03-27-2006, 10:59 AM