To convert a string to an array, we can use the Array() intializer syntax in Swift. Here is an example, that splits the following string into an array of individual characters. Similarly, we can also use the map() function to convert it. The map() function iterates each character in a string.
To split a string into an array in Swift, use the String. split() function.
Xcode 8.1 / Swift 3.0.1 Attention (Swift 4): If you have a string like let a="a,,b,c" and you use a. split(separator: ",") you get an array like ["a", "b", c"] by default. This can be changed using omittingEmptySubsequences: false which is true by default. Any multi-character splits in Swift 4+?
this should be as simple as let characterFromString = Character(textField. text) . NSString is automatically bridged to Swift's String , and the Character class has an init which accepts a single character String ; see the documentation here.
It is even easier in Swift:
let string : String = "Hello 🐶🐮 🇩🇪"
let characters = Array(string)
println(characters)
// [H, e, l, l, o, , 🐶, 🐮, , 🇩🇪]
This uses the facts that
Array
can be created from a SequenceType
, andString
conforms to the SequenceType
protocol, and its sequence generator
enumerates the characters.And since Swift strings have full support for Unicode, this works even with characters outside of the "Basic Multilingual Plane" (such as 🐶) and with extended grapheme clusters (such as 🇩🇪, which is actually composed of two Unicode scalars).
Update: As of Swift 2, String
does no longer conform to
SequenceType
, but the characters
property provides a sequence of the
Unicode characters:
let string = "Hello 🐶🐮 🇩🇪"
let characters = Array(string.characters)
print(characters)
This works in Swift 3 as well.
Update: As of Swift 4, String
is (again) a collection of its
Character
s:
let string = "Hello 🐶🐮 🇩🇪"
let characters = Array(string)
print(characters)
// ["H", "e", "l", "l", "o", " ", "🐶", "🐮", " ", "🇩🇪"]
In Swift 4, you don't have to use characters
to use map()
. Just do map()
on String.
let letters = "ABC".map { String($0) }
print(letters) // ["A", "B", "C"]
print(type(of: letters)) // Array<String>
Or if you'd prefer shorter: "ABC".map(String.init)
(2-bytes 😀)
In Swift 2 and Swift 3, You can use map()
function to characters
property.
let letters = "ABC".characters.map { String($0) }
print(letters) // ["A", "B", "C"]
Accepted answer doesn't seem to be the best, because sequence-converted String
is not a String
sequence, but Character
:
$ swift
Welcome to Swift! Type :help for assistance.
1> Array("ABC")
$R0: [Character] = 3 values {
[0] = "A"
[1] = "B"
[2] = "C"
}
This below works for me:
let str = "ABC"
let arr = map(str) { s -> String in String(s) }
Reference for a global function map()
is here: http://swifter.natecook.com/func/map/
There is also this useful function on String: components(separatedBy: String)
let string = "1;2;3"
let array = string.components(separatedBy: ";")
print(array) // returns ["1", "2", "3"]
Works well to deal with strings separated by a character like ";" or even "\n"
Updated for Swift 4
Here are 3 ways.
//array of Characters
let charArr1 = [Character](myString)
//array of String.element
let charArr2 = Array(myString)
for char in myString {
//char is of type Character
}
In some cases, what people really want is a way to convert a string into an array of little strings with 1 character length each. Here is a super efficient way to do that:
//array of String
var strArr = myString.map { String($0)}
Swift 3
Here are 3 ways.
let charArr1 = [Character](myString.characters)
let charArr2 = Array(myString.characters)
for char in myString.characters {
//char is of type Character
}
In some cases, what people really want is a way to convert a string into an array of little strings with 1 character length each. Here is a super efficient way to do that:
var strArr = myString.characters.map { String($0)}
Or you can add an extension to String.
extension String {
func letterize() -> [Character] {
return Array(self.characters)
}
}
Then you can call it like this:
let charArr = "Cat".letterize()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With