1

i have a issue , i am asking as new question as previoes one was messed

NSString *s=@"hi\nhello\n\nwelcome to this world\ni m jhon"
label.frame = ...//big enough height
label.numberOfLines = 0;
label.text = s;

this code helps me to separate string based on \n

but if i do this

NSString *s=Ad.content //where Ad.content value is **hi\nhello\n\nwelcome to this world\ni m jhon**


label.numberOfLines = 0;
label.text = s;

i am not able to sperate them by \n , what i am doing wrong here kindly suggest

Thanks

3 Answers 3

0

"\n" is not a special token handled by UILabel. It is, in fact, a special token handled by the compiler. When the compiler sees "Hello\nWorld", it converts that into the sequence of characters 'H' 'e' 'l' 'l' 'o' LF 'W' 'o' 'r' 'l' 'd' (where LF is ASCII code 10, or newline). For whatever reason, your Ad.content contains literal "\n" sequences instead of newlines. The best solution is to look at where the content of Ad.content comes from and fix it to actually have real newlines instead of "\n" sequences. If you absolutely must have "\n" sequences, then you can use -[NSMutableString stringByReplacingOccurrencesOfString:withString:]. Of course, if anyone wants to have the literal sequence "\n" show up, then they can't do that. If you want to support generic backslash-escaping (so the literal sequence "\n" would be represented as "\n"), then you can use a more complicated approach with NSScanner where you find each "\", pull out the next character, and handle it appropriately.

My recommendation is to fix Ad.content to contain actual newlines.

1
  • well explanation thanks a lot for me stringByReplacingOccurrencesOfString:withString: works like charm :-)
    – prajakta
    Jan 12, 2011 at 2:37
0
NSString *_stringToSplit = @"hi\nhello\n\nwelcome to this world\ni m jhon";
NSArray *_splitItems = [_stringToSplit componentsSeparatedByString:@"\n"];
NSMutableArray *_mutableSplitItems = [NSMutableArray arrayWithCapacity:[_splitItems count]]; 
[_mutableSplitItems addObjectsFromArray:_splitItems];

[_splitItems count] is your solution

cheers Endo

0

i did this . it was so easy damn i am mad thanks guys

NSString *ss = [aDl.content stringByReplacingOccurrencesOfString:@"\n" withString:@"\n"];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.