Swift Array to String - ios

There is an array like:
let datas =
[["#A1CCE4","", "+0.0%", "+0.0%"],
["#4C3C2F","G", "+1.0%", "+0.2%"],
["#4C3C2F","G", "+3.1%", "+0.6%"],
["#C07155","S", "+0.3%", "+0.1%"],
["#C07155","G", "+2.0%", "+0.4%"],
["#C07155","P", "+1.8%", "+0.3%"],
["#AEB0B3","R", "+2.0%", "+2.0%"]]
How to convert it to string like:
"""let data = [["#A1CCE4", "", "+0.0%", "+0.0%"], ["#4C3C2F", "G", "+1.0%", "+0.2%"], ["#4C3C2F", "G", "+3.1%", "+0.6%"], ["#C07155", "S", "+0.3%", "+0.1%"], ["#C07155", "G", "+2.0%", "+0.4%"], ["#C07155", "P", "+1.8%", "+0.3%"], ["#AEB0B3", "R", "+2.0%", "+2.0%"]]"""
(with "let data = " or not is fine)
EDIT:
"\(datas)" should be the answer.
or
"\(String(describing: datas))"
or
datas.description
Why do I ask this question?
The array actually from the plist, and the number values are from different places.
The number values need to be load in the js code in WKWebView. And acutually the js code is just string.
In my js code I have actually the same code datas variable code.

You actually want to create a JSON string, it's pretty easy with JSONEncoder because [String] conforms to Encodable by default.
let datas =
[["#A1CCE4","", "+0.0%", "+0.0%"],
["#4C3C2F","G", "+1.0%", "+0.2%"],
["#4C3C2F","G", "+3.1%", "+0.6%"],
["#C07155","S", "+0.3%", "+0.1%"],
["#C07155","G", "+2.0%", "+0.4%"],
["#C07155","P", "+1.8%", "+0.3%"],
["#AEB0B3","R", "+2.0%", "+2.0%"]]
do {
let jsonData = try JSONEncoder().encode(datas)
let result = String(data: jsonData, encoding: .utf8)!
print(result)
} catch { print(error) }
It's more reliable than just calling .description on the array.
And only an array literal is directly interchangeable with JSON, a dictionary is not.
If you really need the let datas = prefix just concatenate the strings
let resultWithPrefix = "let datas = " + result

Given your datas array, you can achieve your goal this way:
let datas =
[["#A1CCE4","", "+0.0%", "+0.0%"],
["#4C3C2F","G", "+1.0%", "+0.2%"],
["#4C3C2F","G", "+3.1%", "+0.6%"],
["#C07155","S", "+0.3%", "+0.1%"],
["#C07155","G", "+2.0%", "+0.4%"],
["#C07155","P", "+1.8%", "+0.3%"],
["#AEB0B3","R", "+2.0%", "+2.0%"]]
var str = " let datas = ["
for data in datas{
str += "\(data)"
}
str += "]"
EDIT:
You can use this code, with a loop for each String array in your data, to get a string with comma
var str = " let datas = ["
for data in datas{
str += "["
for s in data{
str += " \" \(s) \" , "
}
str += "],"
}
str += "]"
Another good solution is to use JSONEncoder, as suggested in other answer, I just report it here to complete
let datas = [...]
var myString = "let datas ="
do{
let datasToJson = JSONEncoder().encode(datas)
myString += String(data: datasToJson, encoding: .utf8)!
}catch{
print(error)
}

How about something like this:
let stringStart = "let datas = "
let stringDatas = String(datas)
let finalString = stringStart + stringDatas
More answers here

Related

CSV Parsing - Swift 4

I am trying to parse a CSV but i am getting some issues. Below is the code i used for parsing CSV:
let fileURL = Bundle.main.url(forResource: "test_application_data - Sheet 1", withExtension: "csv")
let content = try String(contentsOf: fileURL!, encoding: String.Encoding.utf8)
let parsedCSV: [[String]] = content.components(separatedBy: "\n").map{ $0.components(separatedBy: ",")}
And this is the data in the CSV i am parsing :
Item 9,Description 9,image url
"Item 10 Extra line 1 Extra line 2 Extra line 3",Description 10,image url
So by using above code i get correct response for first row i.e Item 9 but i am getting malformed response for Item 10
How can i correctly parse both rows?
The RFC for CSV: Common Format and MIME Type for Comma-Separated Values (CSV) Files(RFC-4180)
Not all CSV data or CSV processors conform to all descriptions of this RFC, but generally, fields enclosed within double-quotes can contain:
newlines
commas
escaped double-quotes ("" represents a single double-quote)
This code is a little bit simplified than RFC-4180, but handles all three cases above:
UPDATE This old code does not handle CRLF well. (Which is a valid newline in RFC-4180.) I added a new code at the bottom, please check it.
Thanks to Jay.
import Foundation
let csvText = """
Item 9,Description 9,image url
"Item 10
Extra line 1
Extra line 2
Extra line 3",Description 10,image url
"Item 11
Csv item can contain ""double quote"" and comma(,)", Description 11 ,image url
"""
let pattern = "[ \r\t]*(?:\"((?:[^\"]|\"\")*)\"|([^,\"\\n]*))[ \t]*([,\\n]|$)"
let regex = try! NSRegularExpression(pattern: pattern)
var result: [[String]] = []
var record: [String] = []
let offset: Int = 0
regex.enumerateMatches(in: csvText, options: .anchored, range: NSRange(0..<csvText.utf16.count)) {match, flags, stop in
guard let match = match else {fatalError()}
if match.range(at: 1).location != NSNotFound {
let field = csvText[Range(match.range(at: 1), in: csvText)!].replacingOccurrences(of: "\"\"", with: "\"")
record.append(field)
} else if match.range(at: 2).location != NSNotFound {
let field = csvText[Range(match.range(at: 2), in: csvText)!].trimmingCharacters(in: .whitespaces)
record.append(field)
}
let separator = csvText[Range(match.range(at: 3), in: csvText)!]
switch separator {
case "\n": //newline
result.append(record)
record = []
case "": //end of text
//Ignoring empty last line...
if record.count > 1 || (record.count == 1 && !record[0].isEmpty) {
result.append(record)
}
stop.pointee = true
default: //comma
break
}
}
print(result)
(Intended to test in a Playground.)
New code, CRLF ready.
import Foundation
let csvText = "Field0,Field1\r\n"
let pattern = "[ \t]*(?:\"((?:[^\"]|\"\")*)\"|([^,\"\r\\n]*))[ \t]*(,|\r\\n?|\\n|$)"
let regex = try! NSRegularExpression(pattern: pattern)
var result: [[String]] = []
var record: [String] = []
let offset: Int = 0
regex.enumerateMatches(in: csvText, options: .anchored, range: NSRange(0..<csvText.utf16.count)) {match, flags, stop in
guard let match = match else {fatalError()}
if let quotedRange = Range(match.range(at: 1), in: csvText) {
let field = csvText[quotedRange].replacingOccurrences(of: "\"\"", with: "\"")
record.append(field)
} else if let range = Range(match.range(at: 2), in: csvText) {
let field = csvText[range].trimmingCharacters(in: .whitespaces)
record.append(field)
}
let separator = csvText[Range(match.range(at: 3), in: csvText)!]
switch separator {
case "": //end of text
//Ignoring empty last line...
if record.count > 1 || (record.count == 1 && !record[0].isEmpty) {
result.append(record)
}
stop.pointee = true
case ",": //comma
break
default: //newline
result.append(record)
record = []
}
}
print(result) //->[["Field0", "Field1"]]
The problem is with this line of code:
content.components(separatedBy: "\n")
It separates your csv file into rows based on the newline character. There are newline characters in your "Item 10 Extra line 1 Extra line 2 Extra line 3" String so each extra line is getting treated as a different row, so in the end you get the wrong result.
I'd suggest escaping the newline characters in your multiline text column or getting rid of them altogether. You can also modyfy the input file so the newline delimeter isn't \n at the end of each row but something custom (a string that won't appear elsewhere in the csv file).

Swift: Get all key value pairs from a String

I have a String which has many key value pairs appended by a & sign. i.e: params = key1=Hello&key2=Hello World&key3=Hi Hello
Is there a way to extract the values just by passing the keys present in the string? For example I want to extract the value of key1, key2, key3.
let string = "key1=Hello&key2=Hello World&key3=Hi Hello"
let components = string.components(separatedBy: "&")
var dictionary: [String : String] = [:]
for component in components{
let pair = component.components(separatedBy: "=")
dictionary[pair[0]] = pair[1]
}
And in dictionary you will get your key-value pairs.
You can use URLComponents and URLQueryItem anyway by creating a dummy URL
let params = "key1=Hello&key2=Hello World&key3=Hi Hello"
if let components = URLComponents(string: "http://dummy.com/path?" + params.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)!),
let queryItems = components.queryItems {
let arrayOfValues = queryItems.flatMap{ $0.value }
print(arrayOfValues)
}
see also Best way to parse URL string to get values for keys?
let params = "key1=Hello&key2=Hello World&key3=Hi Hello"
let sepparated = params.components(separatedBy: CharacterSet(charactersIn: "&="))
let keys = sepparated.enumerated().filter{$0.0 % 2 == 0}
print (keys) // [(offset: 0, element: "key1"), (offset: 2, element: "key2"), (offset: 4, element: "key3")]

Create a JSON String in Swift

I send the text from a TextView to my Backend. To include linebreaks, i try to serialize the textView.text to JSON.
let jsonObject: NSMutableDictionary = NSMutableDictionary()
jsonObject.setValue(textView.text, forKey: "text")
let jsonData: NSData
do {
jsonData = try JSONSerialization.data(withJSONObject: jsonObject, options: JSONSerialization.WritingOptions()) as NSData
let jsonString = NSString(data: jsonData as Data, encoding: String.Encoding.utf8.rawValue) as! String
print(jsonString)
} catch _ {
print ("JSON Failure")
}
}
But since this is a Dictionary, the resulting text looks like this:
{"text":"test\nstack\n\noverflow"}
i actually just need: test\nstack\n\noverflow
Is there a pretty way to transform a multiline String in swift to have these "\n" for linebreaks without any extra characters?
Edit:
I expect to type into my textView something like this:
and as a result get the string "hello\n\n"
The source looks like this:
let text = textView.text
let components = text?.components(separatedBy: CharacterSet.newlines).filter({!$0.isEmpty})
let textWithLineFeeds = components?.joined(separator:"\n")
print(textWithLineFeeds!)
print(textView.text)
the first print gives:
test
hello
linebreak above
the second print statement:
test
hello
linebreak above
i wish i could see:
test\nhello\n\nlinebreak above
To convert text with arbitrary new line characters (CR, LF, CRLF etc.) to distinct - only LF - line breaks use:
let text = "test\r\nstack\n\noverflow\rfoo"
let components = text.components(separatedBy: CharacterSet.newlines).filter({!$0.isEmpty})
let textWithLineFeeds = components.joined(separator:"\n")
If the server accepts only CR, change \n to \r.

How do I split a string into an array of characters with Swift? [duplicate]

How can I convert a String "Hello" to an Array ["H","e","l","l","o"] in Swift?
In Objective-C I have used this:
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];
}
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
an Array can be created from a SequenceType, and
String 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
Characters:
let string = "Hello ๐Ÿถ๐Ÿฎ ๐Ÿ‡ฉ๐Ÿ‡ช"
let characters = Array(string)
print(characters)
// ["H", "e", "l", "l", "o", " ", "๐Ÿถ", "๐Ÿฎ", " ", "๐Ÿ‡ฉ๐Ÿ‡ช"]
Edit (Swift 4)
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 ๐Ÿ˜€)
Edit (Swift 2 & Swift 3)
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"]
Original (Swift 1.x)
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"
For Swift version 5.3 its easy as:
let string = "Hello world"
let characters = Array(string)
print(characters)
// ["H", "e", "l", "l", "o", " ", "w", "o", "r", "l", "d"]
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()
An easy way to do this is to map the variable and return each Character as a String:
let someText = "hello"
let array = someText.map({ String($0) }) // [String]
The output should be ["h", "e", "l", "l", "o"].
for the function on String: components(separatedBy: String)
in Swift 5.1
have change to:
string.split(separator: "/")
Martin R answer is the best approach, and as he said, because String conforms the SquenceType protocol, you can also enumerate a string, getting each character on each iteration.
let characters = "Hello"
var charactersArray: [Character] = []
for (index, character) in enumerate(characters) {
//do something with the character at index
charactersArray.append(character)
}
println(charactersArray)
let string = "hell0"
let ar = Array(string.characters)
print(ar)
In Swift 4, as String is a collection of Character, you need to use map
let array1 = Array("hello") // Array<Character>
let array2 = Array("hello").map({ "\($0)" }) // Array<String>
let array3 = "hello".map(String.init) // Array<String>
You can also create an extension:
var strArray = "Hello, playground".Letterize()
extension String {
func Letterize() -> [String] {
return map(self) { String($0) }
}
}
func letterize() -> [Character] {
return Array(self.characters)
}
Suppose you have four text fields otpOneTxt, otpTwoTxt, otpThreeTxt, otpFourTxt and a string getOtp.
let getup = "5642"
let array = self.getOtp.map({ String($0) })
otpOneTxt.text = array[0] //5
otpTwoTxt.text = array[1] //6
otpThreeTxt.text = array[2] //4
otpFourTxt.text = array[3] //2
let str = "cdcd"
let characterArr = str.reduce(into: [Character]()) { result, letter in
result.append(letter)
}
print(characterArr)
//["c", "d", "c", "d"]

Split a String into an array in Swift?

Say I have a string here:
var fullName: String = "First Last"
I want to split the string base on white space and assign the values to their respective variables
var fullNameArr = // something like: fullName.explode(" ")
var firstName: String = fullNameArr[0]
var lastName: String? = fullnameArr[1]
Also, sometimes users might not have a last name.
Just call componentsSeparatedByString method on your fullName
import Foundation
var fullName: String = "First Last"
let fullNameArr = fullName.componentsSeparatedByString(" ")
var firstName: String = fullNameArr[0]
var lastName: String = fullNameArr[1]
Update for Swift 3+
import Foundation
let fullName = "First Last"
let fullNameArr = fullName.components(separatedBy: " ")
let name = fullNameArr[0]
let surname = fullNameArr[1]
The Swift way is to use the global split function, like so:
var fullName = "First Last"
var fullNameArr = split(fullName) {$0 == " "}
var firstName: String = fullNameArr[0]
var lastName: String? = fullNameArr.count > 1 ? fullNameArr[1] : nil
with Swift 2
In Swift 2 the use of split becomes a bit more complicated due to the introduction of the internal CharacterView type. This means that String no longer adopts the SequenceType or CollectionType protocols and you must instead use the .characters property to access a CharacterView type representation of a String instance. (Note: CharacterView does adopt SequenceType and CollectionType protocols).
let fullName = "First Last"
let fullNameArr = fullName.characters.split{$0 == " "}.map(String.init)
// or simply:
// let fullNameArr = fullName.characters.split{" "}.map(String.init)
fullNameArr[0] // First
fullNameArr[1] // Last
The easiest method to do this is by using componentsSeparatedBy:
For Swift 2:
import Foundation
let fullName : String = "First Last";
let fullNameArr : [String] = fullName.componentsSeparatedByString(" ")
// And then to access the individual words:
var firstName : String = fullNameArr[0]
var lastName : String = fullNameArr[1]
For Swift 3:
import Foundation
let fullName : String = "First Last"
let fullNameArr : [String] = fullName.components(separatedBy: " ")
// And then to access the individual words:
var firstName : String = fullNameArr[0]
var lastName : String = fullNameArr[1]
Swift Dev. 4.0 (May 24, 2017)
A new function split in Swift 4 (Beta).
import Foundation
let sayHello = "Hello Swift 4 2017";
let result = sayHello.split(separator: " ")
print(result)
Output:
["Hello", "Swift", "4", "2017"]
Accessing values:
print(result[0]) // Hello
print(result[1]) // Swift
print(result[2]) // 4
print(result[3]) // 2017
Xcode 8.1 / Swift 3.0.1
Here is the way multiple delimiters with array.
import Foundation
let mathString: String = "12-37*2/5"
let numbers = mathString.components(separatedBy: ["-", "*", "/"])
print(numbers)
Output:
["12", "37", "2", "5"]
Update for Swift 5.2 and the simpliest way
let paragraph = "Bob hit a ball, the hit BALL flew far after it was hit. Hello! Hie, How r u?"
let words = paragraph.components(separatedBy: [",", " ", "!",".","?"])
This prints,
["Bob", "hit", "a", "ball", "", "the", "hit", "BALL", "flew", "far",
"after", "it", "was", "hit", "", "Hello", "", "Hie", "", "How", "r",
"u", ""]
However, if you want to filter out empty string,
let words = paragraph.components(separatedBy: [",", " ", "!",".","?"]).filter({!$0.isEmpty})
Output,
["Bob", "hit", "a", "ball", "the", "hit", "BALL", "flew", "far",
"after", "it", "was", "hit", "Hello", "Hie", "How", "r", "u"]
But make sure, Foundation is imported.
Swift 4 or later
If you just need to properly format a person name, you can use PersonNameComponentsFormatter.
The PersonNameComponentsFormatter class provides localized
representations of the components of a personโ€™s name, as represented
by a PersonNameComponents object. Use this class to create localized
names when displaying person name information to the user.
// iOS (9.0 and later), macOS (10.11 and later), tvOS (9.0 and later), watchOS (2.0 and later)
let nameFormatter = PersonNameComponentsFormatter()
let name = "Mr. Steven Paul Jobs Jr."
// personNameComponents requires iOS (10.0 and later)
if let nameComps = nameFormatter.personNameComponents(from: name) {
nameComps.namePrefix // Mr.
nameComps.givenName // Steven
nameComps.middleName // Paul
nameComps.familyName // Jobs
nameComps.nameSuffix // Jr.
// It can also be configured to format your names
// Default (same as medium), short, long or abbreviated
nameFormatter.style = .default
nameFormatter.string(from: nameComps) // "Steven Jobs"
nameFormatter.style = .short
nameFormatter.string(from: nameComps) // "Steven"
nameFormatter.style = .long
nameFormatter.string(from: nameComps) // "Mr. Steven Paul Jobs jr."
nameFormatter.style = .abbreviated
nameFormatter.string(from: nameComps) // SJ
// It can also be use to return an attributed string using annotatedString method
nameFormatter.style = .long
nameFormatter.annotatedString(from: nameComps) // "Mr. Steven Paul Jobs jr."
}
edit/update:
Swift 5 or later
For just splitting a string by non letter characters we can use the new Character property isLetter:
let fullName = "First Last"
let components = fullName.split{ !$0.isLetter }
print(components) // "["First", "Last"]\n"
As an alternative to WMios's answer, you can also use componentsSeparatedByCharactersInSet, which can be handy in the case you have more separators (blank space, comma, etc.).
With your specific input:
let separators = NSCharacterSet(charactersInString: " ")
var fullName: String = "First Last";
var words = fullName.componentsSeparatedByCharactersInSet(separators)
// words contains ["First", "Last"]
Using multiple separators:
let separators = NSCharacterSet(charactersInString: " ,")
var fullName: String = "Last, First Middle";
var words = fullName.componentsSeparatedByCharactersInSet(separators)
// words contains ["Last", "First", "Middle"]
Swift 4
let words = "these words will be elements in an array".components(separatedBy: " ")
The whitespace issue
Generally, people reinvent this problem and bad solutions over and over. Is this a space? " " and what about "\n", "\t" or some unicode whitespace character that you've never seen, in no small part because it is invisible. While you can get away with
A weak solution
import Foundation
let pieces = "Mary had little lamb".componentsSeparatedByString(" ")
If you ever need to shake your grip on reality watch a WWDC video on strings or dates. In short, it is almost always better to allow Apple to solve this kind of mundane task.
Robust Solution: Use NSCharacterSet
The way to do this correctly, IMHO, is to use NSCharacterSet since as stated earlier your whitespace might not be what you expect and Apple has provided a whitespace character set. To explore the various provided character sets check out Apple's NSCharacterSet developer documentation and then, only then, augment or construct a new character set if it doesn't fit your needs.
NSCharacterSet whitespaces
Returns a character set containing the characters in Unicode General
Category Zs and CHARACTER TABULATION (U+0009).
let longerString: String = "This is a test of the character set splitting system"
let components = longerString.components(separatedBy: .whitespaces)
print(components)
In Swift 4.2 and Xcode 10
//This is your str
let str = "This is my String" //Here replace with your string
Option 1
let items = str.components(separatedBy: " ")//Here replase space with your value and the result is Array.
//Direct single line of code
//let items = "This is my String".components(separatedBy: " ")
let str1 = items[0]
let str2 = items[1]
let str3 = items[2]
let str4 = items[3]
//OutPut
print(items.count)
print(str1)
print(str2)
print(str3)
print(str4)
print(items.first!)
print(items.last!)
Option 2
let items = str.split(separator: " ")
let str1 = String(items.first!)
let str2 = String(items.last!)
//Output
print(items.count)
print(items)
print(str1)
print(str2)
Option 3
let arr = str.split {$0 == " "}
print(arr)
Option 4
let line = "BLANCHE: I don't want realism. I want magic!"
print(line.split(separator: " "))
// Prints "["BLANCHE:", "I", "don\'t", "want", "realism.", "I", "want", "magic!"]"
By Apple Documentation....
let line = "BLANCHE: I don't want realism. I want magic!"
print(line.split(separator: " "))
// Prints "["BLANCHE:", "I", "don\'t", "want", "realism.", "I", "want", "magic!"]"
print(line.split(separator: " ", maxSplits: 1))//This can split your string into 2 parts
// Prints "["BLANCHE:", " I don\'t want realism. I want magic!"]"
print(line.split(separator: " ", maxSplits: 2))//This can split your string into 3 parts
print(line.split(separator: " ", omittingEmptySubsequences: false))//array contains empty strings where spaces were repeated.
// Prints "["BLANCHE:", "", "", "I", "don\'t", "want", "realism.", "I", "want", "magic!"]"
print(line.split(separator: " ", omittingEmptySubsequences: true))//array not contains empty strings where spaces were repeated.
print(line.split(separator: " ", maxSplits: 4, omittingEmptySubsequences: false))
print(line.split(separator: " ", maxSplits: 3, omittingEmptySubsequences: true))
Only the split is the correct answer, here are the difference for more than 2 spaces.
Swift 5
var temp = "Hello world ni hao"
let arr = temp.components(separatedBy: .whitespacesAndNewlines)
// ["Hello", "world", "", "", "", "", "ni", "hao"]
let arr2 = temp.components(separatedBy: " ")
// ["Hello", "world", "", "", "", "", "ni", "hao"]
let arr3 = temp.split(whereSeparator: {$0 == " "})
// ["Hello", "world", "ni", "hao"]
Swift 4 makes it much easier to split characters, just use the new split function for Strings.
Example:
let s = "hi, hello"
let a = s.split(separator: ",")
print(a)
Now you got an array with 'hi' and ' hello'.
Swift 3
let line = "AAA BBB\t CCC"
let fields = line.components(separatedBy: .whitespaces).filter {!$0.isEmpty}
Returns three strings AAA, BBB and CCC
Filters out empty fields
Handles multiple spaces and tabulation characters
If you want to handle new lines, then replace .whitespaces with .whitespacesAndNewlines
Swift 4, Xcode 10 and iOS 12 Update 100% working
let fullName = "First Last"
let fullNameArr = fullName.components(separatedBy: " ")
let firstName = fullNameArr[0] //First
let lastName = fullNameArr[1] //Last
See the Apple's documentation here for further information.
Xcode 8.0 / Swift 3
let fullName = "First Last"
var fullNameArr = fullName.components(separatedBy: " ")
var firstname = fullNameArr[0] // First
var lastname = fullNameArr[1] // Last
Long Way:
var fullName: String = "First Last"
fullName += " " // this will help to see the last word
var newElement = "" //Empty String
var fullNameArr = [String]() //Empty Array
for Character in fullName.characters {
if Character == " " {
fullNameArr.append(newElement)
newElement = ""
} else {
newElement += "\(Character)"
}
}
var firsName = fullNameArr[0] // First
var lastName = fullNameArr[1] // Last
Most of these answers assume the input contains a space - not whitespace, and a single space at that. If you can safely make that assumption, then the accepted answer (from bennett) is quite elegant and also the method I'll be going with when I can.
When we can't make that assumption, a more robust solution needs to cover the following siutations that most answers here don't consider:
tabs/newlines/spaces (whitespace), including recurring characters
leading/trailing whitespace
Apple/Linux (\n) and Windows (\r\n) newline characters
To cover these cases this solution uses regex to convert all whitespace (including recurring and Windows newline characters) to a single space, trims, then splits by a single space:
Swift 3:
let searchInput = " First \r\n \n \t\t\tMiddle Last "
let searchTerms = searchInput
.replacingOccurrences(
of: "\\s+",
with: " ",
options: .regularExpression
)
.trimmingCharacters(in: .whitespaces)
.components(separatedBy: " ")
// searchTerms == ["First", "Middle", "Last"]
I had a scenario where multiple control characters can be present in the string I want to split. Rather than maintain an array of these, I just let Apple handle that part.
The following works with Swift 3.0.1 on iOS 10:
let myArray = myString.components(separatedBy: .controlCharacters)
I found an Interesting case, that
method 1
var data:[String] = split( featureData ) { $0 == "\u{003B}" }
When I used this command to split some symbol from the data that loaded from server, it can split while test in simulator and sync with test device, but it won't split in publish app, and Ad Hoc
It take me a lot of time to track this error, It might cursed from some Swift Version, or some iOS Version or neither
It's not about the HTML code also, since I try to stringByRemovingPercentEncoding and it's still not work
addition 10/10/2015
in Swift 2.0 this method has been changed to
var data:[String] = featureData.split {$0 == "\u{003B}"}
method 2
var data:[String] = featureData.componentsSeparatedByString("\u{003B}")
When I used this command, it can split the same data that load from server correctly
Conclusion, I really suggest to use the method 2
string.componentsSeparatedByString("")
Steps to split a string into an array in Swift 4.
assign string
based on # splitting.
Note: variableName.components(separatedBy: "split keyword")
let fullName: String = "First Last # triggerd event of the session by session storage # it can be divided by the event of the trigger."
let fullNameArr = fullName.components(separatedBy: "#")
print("split", fullNameArr)
This gives an array of split parts directly
var fullNameArr = fullName.components(separatedBy:" ")
then you can use like this,
var firstName: String = fullNameArr[0]
var lastName: String? = fullnameArr[1]
Or without closures you can do just this in Swift 2:
let fullName = "First Last"
let fullNameArr = fullName.characters.split(" ")
let firstName = String(fullNameArr[0])
Swift 4
let string = "loremipsum.dolorsant.amet:"
let result = string.components(separatedBy: ".")
print(result[0])
print(result[1])
print(result[2])
print("total: \(result.count)")
Output
loremipsum
dolorsant
amet:
total: 3
The simplest solution is
let fullName = "First Last"
let components = fullName.components(separatedBy: .whitespacesAndNewlines).compactMap { $0.isEmpty ? nil : $0 }
This will handled multiple white spaces in a row of different types (white space, tabs, newlines etc) and only returns a two element array, you can change the CharacterSet to include more character you like, if you want to get cleaver you can use Regular Expression Decoder, this lets you write regular expression that can be used to decoded string directly into your own class/struct that implement the Decoding protocol. For something like this is over kill, but if you are using it as an example for more complicate string it may make more sense.
Let's say you have a variable named "Hello World" and if you want to split it and store it into two different variables you can use like this:
var fullText = "Hello World"
let firstWord = fullText.text?.components(separatedBy: " ").first
let lastWord = fullText.text?.components(separatedBy: " ").last
This has Changed again in Beta 5. Weee! It's now a method on CollectionType
Old:
var fullName = "First Last"
var fullNameArr = split(fullName) {$0 == " "}
New:
var fullName = "First Last"
var fullNameArr = fullName.split {$0 == " "}
Apples Release Notes
String handling is still a challenge in Swift and it keeps changing significantly, as you can see from other answers. Hopefully things settle down and it gets simpler. This is the way to do it with the current 3.0 version of Swift with multiple separator characters.
Swift 3:
let chars = CharacterSet(charactersIn: ".,; -")
let split = phrase.components(separatedBy: chars)
// Or if the enums do what you want, these are preferred.
let chars2 = CharacterSet.alphaNumerics // .whitespaces, .punctuation, .capitalizedLetters etc
let split2 = phrase.components(separatedBy: chars2)
I was looking for loosy split, such as PHP's explode where empty sequences are included in resulting array, this worked for me:
"First ".split(separator: " ", maxSplits: 1, omittingEmptySubsequences: false)
Output:
["First", ""]
let str = "one two"
let strSplit = str.characters.split(" ").map(String.init) // returns ["one", "two"]
Xcode 7.2 (7C68)
Swift 2.2
Error Handling & capitalizedString Added :
func setFullName(fullName: String) {
var fullNameComponents = fullName.componentsSeparatedByString(" ")
self.fname = fullNameComponents.count > 0 ? fullNameComponents[0]: ""
self.sname = fullNameComponents.count > 1 ? fullNameComponents[1]: ""
self.fname = self.fname!.capitalizedString
self.sname = self.sname!.capitalizedString
}
OFFTOP:
For people searching how to split a string with substring (not a character), then here is working solution:
// TESTING
let str1 = "Hello user! What user's details? Here user rounded with space."
let a = str1.split(withSubstring: "user") // <-------------- HERE IS A SPLIT
print(a) // ["Hello ", "! What ", "\'s details? Here ", " rounded with space."]
// testing the result
var result = ""
for item in a {
if !result.isEmpty {
result += "user"
}
result += item
}
print(str1) // "Hello user! What user's details? Here user rounded with space."
print(result) // "Hello user! What user's details? Here user rounded with space."
print(result == str1) // true
/// Extension providing `split` and `substring` methods.
extension String {
/// Split given string with substring into array
/// - Parameters:
/// - string: the string
/// - substring: the substring to search
/// - Returns: array of components
func split(withSubstring substring: String) -> [String] {
var a = [String]()
var str = self
while let range = str.range(of: substring) {
let i = str.distance(from: str.startIndex, to: range.lowerBound)
let j = str.distance(from: str.startIndex, to: range.upperBound)
let left = str.substring(index: 0, length: i)
let right = str.substring(index: j, length: str.length - j)
a.append(left)
str = right
}
if !str.isEmpty {
a.append(str)
}
return a
}
/// the length of the string
public var length: Int {
return self.count
}
/// Get substring, e.g. "ABCDE".substring(index: 2, length: 3) -> "CDE"
///
/// - parameter index: the start index
/// - parameter length: the length of the substring
///
/// - returns: the substring
public func substring(index: Int, length: Int) -> String {
if self.length <= index {
return ""
}
let leftIndex = self.index(self.startIndex, offsetBy: index)
if self.length <= index + length {
return String(self[leftIndex..<self.endIndex])
}
let rightIndex = self.index(self.endIndex, offsetBy: -(self.length - index - length))
return String(self[leftIndex..<rightIndex])
}
}

Resources