[英]NSString to NSArray
I want to split an NSString
into an NSArray
.我想将
NSString
拆分为NSArray
。 For example, given:例如,给定:
NSString *myString=@"ABCDEF";
I want an NSArray
like:我想要一个像这样的
NSArray
:
NSArray *myArray={A,B,C,D,E,F};
How to do this with Objective-C and Cocoa?如何用 Objective-C 和 Cocoa 做到这一点?
NSMutableArray *letterArray = [NSMutableArray array];
NSString *letters = @"ABCDEF𝍱क्";
[letters enumerateSubstringsInRange:NSMakeRange(0, [letters length])
options:(NSStringEnumerationByComposedCharacterSequences)
usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
[letterArray addObject:substring];
}];
for (NSString *i in letterArray){
NSLog(@"%@",i);
}
results in结果是
A
B
C
D
E
F
𝍱
क्
enumerateSubstringsInRange:options:usingBlock:
available for iOS 4+ can enumerate a string with different styles. enumerateSubstringsInRange:options:usingBlock:
适用于 iOS 4+可以枚举不同 styles 的字符串。 One is called NSStringEnumerationByComposedCharacterSequences
, what will enumerate letter by letter but is sensitive to surrogate pairs, base characters plus combining marks, Hangul jamo, and Indic consonant clusters , all referred as Composed Character一个叫做
NSStringEnumerationByComposedCharacterSequences
,它会逐个字母地枚举,但对代理对、基本字符加上组合标记、Hangul jamo 和 Indic 辅音簇很敏感,所有这些都称为Composed Character
Note, that the accepted answer "swallows"请注意,接受的答案“吞下”
and breaks
क्
into क
and ्
.并将
क्
分解为क
和्
。
Conversion转换
NSString * string = @"A B C D E F";
NSArray * array = [string componentsSeparatedByString:@" "];
//Notice that in this case I separated the objects by a space because that's the way they are separated in the string
Logging日志记录
NSLog(@"%@", array);
This is what the console returned这是控制台返回的内容
NSMutableArray *chars = [[NSMutableArray alloc] initWithCapacity:[theString length]];
for (int i=0; i < [theString length]; i++) {
NSString *ichar = [NSString stringWithFormat:@"%C", [theString characterAtIndex:i]];
[chars addObject:ichar];
}
This link contains examples to split a string into a array based on sub strings and also based on strings in a character set. 此链接包含基于子字符串以及基于字符集中的字符串将字符串拆分为数组的示例。 I hope that post may help you.
我希望那个帖子可以帮助你。
here is the code snip这是代码片段
NSMutableArray *characters = [[NSMutableArray alloc] initWithCapacity:[myString length]];
for (int i=0; i < [myString length]; i++) {
NSString *ichar = [NSString stringWithFormat:@"%c", [myString characterAtIndex:i]];
[characters addObject:ichar];
}
Without loop you can use this:没有循环你可以使用这个:
NSString *myString = @"ABCDEF";
NSMutableString *tempStr =[[NSMutableString alloc] initWithString:myString];
if([myString length] != 0)
{
NSError *error = NULL;
// declare regular expression object
NSRegularExpression *regex =[NSRegularExpression regularExpressionWithPattern:@"(.)" options:NSMatchingReportCompletion error:&error];
// replace each match with matches character + <space> e.g. 'A' with 'A '
[regex replaceMatchesInString:tempStr options:NSMatchingReportCompletion range:NSMakeRange(0,[myString length]) withTemplate:@"$0 "];
// trim last <space> character
[tempStr replaceCharactersInRange:NSMakeRange([tempStr length] - 1, 1) withString:@""];
// split into array
NSArray * arr = [tempStr componentsSeparatedByString:@" "];
// print
NSLog(@"%@",arr);
}
This solution append space in front of each character with the help of regular expression and uses componentsSeparatedByString
with <space>
to return an array此解决方案 append 借助正则表达式在每个字符前面留有空格,并使用
componentsSeparatedByString
和<space>
返回一个数组
Swift 4.2: Swift 4.2:
String to Array字符串到数组
let list = "Karin, Carrie, David"
let listItems = list.components(separatedBy: ", ")
Output: ["Karin", "Carrie", "David"] Output:[“卡琳”,“嘉莉”,“大卫”]
Array to String数组转字符串
let list = ["Karin", "Carrie", "David"]
let listStr = list.joined(separator: ", ")
Output: "Karin, Carrie, David" Output:“卡琳,嘉莉,大卫”
In Swift, this becomes very simple.在 Swift 中,这变得非常简单。
Swift 3: Swift 3:
myString.characters.map { String($0) }
Swift 4: Swift 4:
myString.map { String($0) }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.