我有一个字符串:"mocktail, wine, beer"
如何将其转换为:"mocktail", "wine", "beer"
?
答案 0 :(得分:5)
以下内容为您提供了所需的结果:
NSString *_inputString = @"\"mocktail, wine, beer\"";
NSLog(@"input string : %@", _inputString);
NSLog(@"output string : %@", [_inputString stringByReplacingOccurrencesOfString:@", " withString:@"\", \""]);
结果是:
input string : "mocktail, wine, beer"
output string : "mocktail", "wine", "beer"
答案 1 :(得分:2)
您需要使用:
NSArray * components = [myString componentsSeparatedByString: @", "];
答案 2 :(得分:0)
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]];
}
答案 3 :(得分:-1)
如果不清楚地了解iOS或objective-c,我假设您可以使用拆分功能。
在几乎任何更高级别的编程语言中都有这样的功能。
这会为您提供一系列字符串。然后,您可以实际处理您想要做的事情,例如:用单引号包围它们并将它们重新附加在一起。 :d