-1

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?

Community
  • 1
  • 1
Nikunj Jadav
  • 3,417
  • 7
  • 38
  • 54

4 Answers4

3

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.

jrturton
  • 118,105
  • 32
  • 252
  • 268
0

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.

Alex Terente
  • 12,006
  • 5
  • 51
  • 71
0

you can use like :

NSString * str  = @"Hello,How are you,Norman,Stanley,Fletcher";

yourLabel.text = [str stringByReplacingOccurrencesOfString:@"," withString:@"\n"]);
Gyani
  • 2,241
  • 1
  • 24
  • 38
0

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.

Lokus001
  • 159
  • 1
  • 5