Fahim Parkar
Fahim Parkar

Reputation: 31647

substringToIndex is not giving correct output even after trimming

I have below.

NSString *myStxt = [postText.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
myStxt = [myStxt stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];
myStxt = [myStxt stringByReplacingOccurrencesOfString:@"\r" withString:@""];
myStxt = [myStxt stringByReplacingOccurrencesOfString:@"\t" withString:@""];

NSLog(@"myStxt>>%@<<", [myStxt substringToIndex:1]);

postText.text contains arabic string which I have assigned via webservice

postText.text = [[myArray objectAtIndex:0] valueForKey:@"myVariable"];

It have wording as ‏رونالدينهو : الكرة الذهبية الخامسة لميسي رائعه و هي من ضمن تلك الأشياء التي تُعطيني سعاده اكثر

Now when I run above code below are the result...

myStxt>><<  --> NSLog(@"myStxt>>%@<<", [myStxt substringToIndex:1]);
myStxt>>ر<<  --> NSLog(@"myStxt>>%@<<", [myStxt substringToIndex:2]);

What I was expecting was as below.

myStxt>>ر<<  --> NSLog(@"myStxt>>%@<<", [myStxt substringToIndex:1]);
myStxt>>رو<<  --> NSLog(@"myStxt>>%@<<", [myStxt substringToIndex:2]);

Any idea why this is giving me wrong details?


This is link for JSON Data

Second object is giving me this issue, but 3rd object is working fine.


Edit 1

For below code

for (int i=0;i<myStxt.length;i++) {
    NSLog(@"mmm==%d==%@", i, [myStxt substringWithRange:NSMakeRange(i,1)]);
}

Below is the output

2016-02-02 12:01:40.587 myapp[91747:12700140] mmm==0==‏
2016-02-02 12:01:40.587 myapp[91747:12700140] mmm==1==ر
2016-02-02 12:01:40.588 myapp[91747:12700140] mmm==2==و
2016-02-02 12:01:40.588 myapp[91747:12700140] mmm==3==ن
2016-02-02 12:01:40.588 myapp[91747:12700140] mmm==4==ا
2016-02-02 12:01:40.588 myapp[91747:12700140] mmm==5==ل
2016-02-02 12:01:40.588 myapp[91747:12700140] mmm==6==د
2016-02-02 12:01:40.588 myapp[91747:12700140] mmm==7==ي
...... and so on...

Upvotes: 1

Views: 72

Answers (3)

Sulthan
Sulthan

Reputation: 130132

After loading your JSON, it became obvious that the first character in your text is RIGHT-TO-LEFT MARK which is a format character so it is not removed when you are trimming whitespace characters.

You can remove it using:

text = [text stringByTrimmingCharactersInSet:[NSCharacterSet controlCharacterSet]];

To test it by yourself, you can use the following code:

for (NSUInteger i = 0; i < text.length; i++) {
    NSLog(@"Character at index %@: \"%i\"", @(i), [text characterAtIndex:i]);
}

which is printing the integer values of characters.

Upvotes: 1

gnasher729
gnasher729

Reputation: 52592

Did you try printing all the characters of the string, one by one, and what was the result? Did you try printing the first five characters so you have a chance to uniquely identify what part of your input was printed?

At the moment I can only guess that your string is a mixture of latin and arabic (as indicated by the colon in the middle) and the start of the string is nowhere near where you think it is.

And of course if you have text with lines separated by CR or LF, the first character is the one that you see at the right side of the first line. After deleting CR and LF that will still be the first character. So most likely nothing is wrong with the code; but your expectations are wrong.

Upvotes: 0

Reshmi Majumder
Reshmi Majumder

Reputation: 961

May be some space is present in your code of arabic words here is my code it is running fine

 NSString *str =@"رونالدينهو : الكرة الذهبية الخامسة لميسي رائعه و هي من ضمن تلك الأشياء التي تُعطيني سعاده اكث";
NSLog(@"myStxt>>%@<<", [str substringToIndex:1]);
NSLog(@"myStxt>>%@<<", [str substringToIndex:2]);

Debugged: myStxt>>ر<<

myStxt>>رو<<

Upvotes: 0

Related Questions