16

How can I split the string @"Hello" to either:

  • a C array of 'H', 'e', 'l', 'l', 'o'

or:

  • an Objective-C array of @[@"H", @"e", @"l", @"l", @"o"]
Cœur
  • 37,241
  • 25
  • 195
  • 267
ytpm
  • 4,962
  • 6
  • 56
  • 113
  • 1
    Take a look at [NSString get -characters](http://stackoverflow.com/questions/3886882/nsstring-get-characters) and [How to get a single NSString character from an NSString](http://stackoverflow.com/questions/3836670/how-to-get-a-single-nsstring-character-from-an-nsstring) post. – Parag Bafna Feb 05 '12 at 19:17

4 Answers4

38

If you're satisfied with a C array of chars, try:

const char *array = [@"Hello" UTF8String];

If you need an NSArray, try:

NSMutableArray *array = [NSMutableArray array];
NSString *str = @"Hello";
for (int i = 0; i < [str length]; i++) {
    NSString *ch = [str substringWithRange:NSMakeRange(i, 1)];
    [array addObject:ch];
}

And array will contain each character as an element of it.

  • is there a command to replace a letter in array? like [array replaceObjectAtIndex:0] hah, something like that? if i want to replace something in array index? – ytpm Feb 05 '12 at 21:38
  • 3
    @H2CO3 instead of using substring with a range of 1, just use `-characterAtIndex:`. – Jack Lawrence May 15 '12 at 19:13
  • And how do you add the non-object `char' to the array? –  May 15 '12 at 19:14
  • Loop your `const char *array` with: `for (int i; i < sizeof(array); i++) { doSomethingWith(array[i]); }` – LenArt Aug 05 '14 at 06:18
9

Try this :

- (void) testCode
{
    NSString *tempDigit = @"12345abcd" ;
    NSMutableArray *tempArray = [NSMutableArray array];
    [tempDigit enumerateSubstringsInRange:[tempDigit rangeOfString:tempDigit]
                                  options:NSStringEnumerationByComposedCharacterSequences
                               usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
            [tempArray addObject:substring] ;
        }] ;

    NSLog(@"tempArray = %@" , tempArray);
}
John
  • 2,640
  • 1
  • 16
  • 16
4

You can use - (unichar)characterAtIndex:(NSUInteger)index to access the string characters at each index.

So,

NSString* stringie = @"astring";
NSUInteger length = [stringie length];
unichar stringieChars[length];
for( unsigned int pos = 0 ; pos < length ; ++pos )
{
    stringieChars[pos] = [stringie characterAtIndex:pos];
}
// replace the 4th element of stringieChars with an 'a' character
stringieChars[3] = 'a';
// print the modified array you produced from the NSString*
NSLog(@"%@",[NSString stringWithCharacters:stringieChars length:length]);
Thomson Comer
  • 3,919
  • 3
  • 30
  • 32
  • is there a command to replace a letter in array? like [array replaceObjectAtIndex:0] hah, something like that? if i want to replace something in array index? – ytpm Feb 05 '12 at 21:38
  • Now you've got a basic C array that you can make changes to index by index. I changed my answer to reflect your added question. – Thomson Comer Feb 05 '12 at 22:41
0

A user529758 mentions, split your string - the C way - like:

const char *array = [@"Hello" UTF8String];

But then loop it using:

for (int i = 0; i < sizeof(array); i++) {
  doSomethingWithCharacter(array[i]);
}
LenArt
  • 333
  • 3
  • 6
  • remember that `const char *array` from strings may end with the NUL character for terminating strings `'\0'`, so your size may be 1 larger than the amount of readable characters. http://en.wikipedia.org/wiki/Ascii#ASCII_control_code_chart – LenArt Aug 06 '14 at 14:22