I have a string like: "mocktail, wine, beer"
How can I convert this into: "mocktail", "wine", "beer"
?
I have a string like: "mocktail, wine, beer"
How can I convert this into: "mocktail", "wine", "beer"
?
the following gives you the desired result:
NSString *_inputString = @"\"mocktail, wine, beer\"";
NSLog(@"input string : %@", _inputString);
NSLog(@"output string : %@", [_inputString stringByReplacingOccurrencesOfString:@", " withString:@"\", \""]);
the result is:
input string : "mocktail, wine, beer"
output string : "mocktail", "wine", "beer"
You need to use:
NSArray * components = [myString componentsSeparatedByString: @", "];
NSString *string = @"mocktail, wine, beer";
//remove whitespaces
NSString *trimmedString = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
//get array of string
NSArray *array = [trimmedString componentsSeparatedByString:@","];
NSMutableArray *newArray = [[NSMutableArray alloc] init];
for (NSString *trimmedString in array) {
NSString *newString = [NSMutableString stringWithFormat:@"'%@'", trimmedString];
[newArray addObject:newString];
}
//merge new strings
NSString *finalString = [NSString stringWithFormat:@"%@", [newArray objectAtIndex:0]];
for (NSInteger i = 1; i < [newArray count]; i++) {
finalString = [NSString stringWithFormat:@"%@, %@", finalString, [newArray objectAtIndex:i]];
}
Without knowing spesifically about iOS or objective-c, I assume you could use a split function.
In almost any higher level programming language there is such a function.
Try: Objective-C split
This gets you an array of Strings. You can then practically do with those what you want to do, e.g. surrounding them with single quotes and appending them back together. :D