0

Suppose I have a String - "Hello" How can I split the String into an Array which holds each character like this - ["H", "e", "l", "l", "o"] Thanks.

Krish Wadhwana
  • 1,544
  • 2
  • 12
  • 24
  • This is actually an **_exact_** duplicate of the question luk2302 mentioned. Even the example string is the same – Arc676 Oct 17 '15 at 14:10
  • But the wordings of the title is more perfect. May be thats why 15+ people marked as useful. – Vin May 18 '22 at 08:03

2 Answers2

16

The characters in a string is put under the characters collection. Just iterate over them and convert them to String:

Swift 4

let str =  "Hello"
let arr = str.map { String($0) }
print(arr)

Swift 3

let str =  "Hello"
let arr = str.characters.map { String($0) }
print(arr)
Code Different
  • 90,614
  • 16
  • 144
  • 163
  • 2
    Or just `Array(str.characters)` – Arc676 Oct 17 '15 at 14:11
  • 2
    @Arc676 the difference is that your code yields a Character Array while his answer yields a String Array – luk2302 Oct 17 '15 at 14:12
  • `Array(str.characters)` creates an array of `Character`. Harder to work with down the road – Code Different Oct 17 '15 at 14:12
  • 1
    Just to clarify for those reading this: The expression `str.characters` gives an array of `Character` structs. The code `str.characters.map { String($0) }` creates an array of characters and then maps that array back to an array of single-charater `String` objects. – Duncan C Oct 17 '15 at 14:19
-4

I think you'll have to do it somewhat manually.

NSString *str = @"Hello";
NSMutableArray *chars = [NSMutableArray new];
for (NSUInteger i = 0; i < [str length]; i++) {
    unichar c = [str characterAtIndex:i];
    [chars addObject:[NSString stringWithCharacters:&c length:1]]; 
}
l00phole
  • 622
  • 3
  • 8
  • Question is asked for Swift.. – itsji10dra Oct 17 '15 at 14:06
  • Wrong language and wrong statement. – luk2302 Oct 17 '15 at 14:06
  • The OP is asking for a solution in Swift, where handling of characters from a string is different, slightly more complicated, and has changed slightly in Swift 2. I'm not that familiar with Swift 2 yet, so I'm going to let somebody who IS up on Swift 2 answer this. – Duncan C Oct 17 '15 at 14:06
  • This is for Obj-C. The question uses Swift 2 (which gives you the array of characters with `str.characters`) – Arc676 Oct 17 '15 at 14:06
  • I still don't follow you. I missed the language thing and assumed Objective-C, but I don't know a better way in Objective-C, do you? – l00phole Oct 17 '15 at 14:08
  • @luk2302 I'll take your silence as "No, I don't know a better way, so iterating manually is the only way in Objective-C". – l00phole Oct 17 '15 at 14:23
  • 1
    Posting a answer in the wrong language and then claiming that your statements are actually true in that (wrong) language is just ... I don't know ... just still very much wrong. If I post a CJam answer and say that you can do that with only one letter - then that is true BUT achieves nothing. So while you might be correct in Objective-C that does just simply not matter. – luk2302 Oct 17 '15 at 14:26
  • @luk2302 Yes, I've acknowledged my answer as being for the wrong language. It's the "wrong statement" part of your comment I am asking about and you have yet to demonstrate it as incorrect (again, for Objective-C). – l00phole Oct 17 '15 at 14:27
  • It isn't incorrect for Objective-C but it is incorrect for the question asked since Objective-C is irrelevant here. – curiously77 Aug 25 '20 at 19:31