gopal
gopal

Reputation: 212

NSString to NSArray

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?

Upvotes: 11

Views: 20108

Answers (7)

Harikant Amipara
Harikant Amipara

Reputation: 191

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"

Upvotes: 0

KPM
KPM

Reputation: 10608

In Swift, this becomes very simple.

Swift 3:

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

Swift 4:

myString.map { String($0) }

Upvotes: -1

vikingosegundo
vikingosegundo

Reputation: 52237

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 .

Upvotes: 25

EmptyStack
EmptyStack

Reputation: 51374

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

Upvotes: 4

user1873574
user1873574

Reputation: 93

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

Upvotes: 1

Fernando Cervantes
Fernando Cervantes

Reputation: 2962

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);

Console

This is what the console returned

Upvotes: 12

yan.kun
yan.kun

Reputation: 6908

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

Upvotes: 11

Related Questions