0

I am receiving the data in an iPad application from a socket connected. I am converting the data received to NSString using the method below:

NSString *data = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding].

Then I am creating a substring from the string using the

NSString *substring1 = [data substringFromIndex:length-9]
NSString *substring2 = [data substringFromIndex:length-3]

where length is [data length].

Then I am comparing the substring2 with @"/>" string as below [substring2 compare:@"/>"] Here I checked the value of the substring2 while debugging the application the value is @"/>" but the comparison result is returned as NSOrderedDescending instead of NSOrderedSame.

Can anyone please help?

Akhilrajtr
  • 5,170
  • 3
  • 19
  • 30

1 Answers1

0

Your string is having trailing space. The string which you are extracting as length - 3, it must be of length 3.

Now you are comparing it with @"/>" which is having length 2.

You need to do it be below way:

NSString *data = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding].
data = [data stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

Now take the substring and compare.

Apurv
  • 17,116
  • 8
  • 51
  • 67