どうも、ちょげ(@chogetarou)です。
文字列の空白を置換する方法を紹介します。
方法

文字列の空白を置換するには、replaceOccurrencesメソッドを使います。
まず、文字列からreplaceOccurancesメソッドを呼び出します。
そして、replaceOccurrencesメソッドの第1引数の空白文字、引数「with」に文字列を指定します。
var replace = str.replacingOccurrences(of: " ", with: "replace")
上記のreplaceOccurencesメソッドは、空白を引数「with」の文字に置換します。
使用例
let str = "Apple Banan Strawberry"
var replace = str.replacingOccurrences(of: " ", with: ",") //半角の空白を置換
replace = str.replacingOccurrences(of: " ", with: ",") //全角の空白を置換
print(replace) //Apple,Banan,Strawberry
オススメの記事
コメント