Possible Duplicate:
Split up NSString using a comma
I have an NSString like Hello,How are you,Norman,Stanley,Fletcher
so I want to split that string when comma separator is occur and that string set into UILabel in iPhone.
How can I do this?
Possible Duplicate:
Split up NSString using a comma
I have an NSString like Hello,How are you,Norman,Stanley,Fletcher
so I want to split that string when comma separator is occur and that string set into UILabel in iPhone.
How can I do this?
Original string:
NSString originalString = @"Hello,How are you,Norman,Stanley,Fletcher";
Split into multiple lines:
NSString multiLineString = [originalString stringByReplacingOccurrencesOfString:@"," withString:"@\n"];
Assign to your label:
label.text = mutliLineString;
Need to make sure the label takes multiple lines:
label.numberOfLines = 0;
Will make it display as many lines as required.
You can use componentsSeparatedByString: method do divide the string. This method will return an array of NSStrings. You can display them in to an UITextView with editable property set to no.
you can use like :
NSString * str = @"Hello,How are you,Norman,Stanley,Fletcher";
yourLabel.text = [str stringByReplacingOccurrencesOfString:@"," withString:@"\n"]);
To split your @"Hello,How are you" use the following code :
NSString *string = @"Hello,How are you";
NSArray *array = [string componentsSeparatedByString: @","];
And to check if there is a comma in a string use this :
NSRange matchNotFound;
matchNotFound = [[label text] rangeOfString: @","];
if ((matchNotFound.location != NSNotFound) {
//if there is a comma
} else {
//if there is no comma
}
Hopping find this useful.