NSString to NSArray

。_饼干妹妹 提交于 2019-12-28 02:15:12

问题


I want to split an NSString into an NSArray. For example, given:

NSString *myString=@"ABCDEF";

I want an NSArray like:

NSArray *myArray={A,B,C,D,E,F};

How to do this with Objective-C and Cocoa?


回答1:


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. 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

Note, that the accepted answer "swallows" 𝍱and breaks क् into and .




回答2:


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




回答3:


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];
}



回答4:


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];
}



回答5:


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




回答6:


Swift 4.2:

String to Array

let list = "Karin, Carrie, David"

let listItems = list.components(separatedBy: ", ")

Output : ["Karin", "Carrie", "David"]

Array to String

let list = ["Karin", "Carrie", "David"]

let listStr = list.joined(separator: ", ")

Output : "Karin, Carrie, David"




回答7:


In Swift, this becomes very simple.

Swift 3:

myString.characters.map { String($0) }

Swift 4:

myString.map { String($0) }


来源:https://stackoverflow.com/questions/6579728/nsstring-to-nsarray

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!