-2

I did following experiment. Can any one point out why strings initialized with stringwithformat fail in string searching?

    NSString *test1 = @"Hello";
    NSString *test2 = @"Hello";
    NSString *test3 = [NSString stringWithFormat:@"%@ ", test2];

   NSRange titleResultsRange = [test1 rangeOfString:test2 options:NSCaseInsensitiveSearch];

I get titleResultsRange.length > 0

But when I do -

NSRange titleResultsRange = [test1 rangeOfString:test3 options:NSCaseInsensitiveSearch];

I get titleResultsRange.length = 0

Why?

AusCBloke
  • 18,014
  • 6
  • 40
  • 44
user1041086
  • 121
  • 1
  • 3
  • 10
  • 2
    Note that `test3` has an extra white space. –  Nov 11 '11 at 05:26
  • Yes, even then NSRange.length should be greater than 0. As there is a substring match. Do you get the same result? – user1041086 Nov 12 '11 at 02:30
  • There is no match since `test3` is not a substring of `test1` because, as I said earlier, `test3` has an extra white space. Maybe you’re confusing the receiver and the argument. –  Nov 12 '11 at 02:55

2 Answers2

1

Could it be that test3 is "Hello " not "Hello".

Jim Rhodes
  • 5,021
  • 4
  • 25
  • 38
0
NSString *test1 = @"Hello";
NSString *test2 = @"Hello";
NSString *test3 = [NSString stringWithFormat:@"%@", test2];
NSRange titleResultsRange = [test1 rangeOfString:test2 options:NSCaseInsensitiveSearch];

Try now. In your code test3 string contain extra white space.

Parag Bafna
  • 22,812
  • 8
  • 71
  • 144