Swiftで半角空白文字削除(trim)
半角空白削除の場合には、whitespaces、改行などの制御記号も取り除く場合にはNewlinesも追加する。
let myString = " \t\t Let's trim all the whitespace \n \t \n " let trimmedString = myString.trimmingCharacters(in: .whitespacesAndNewlines) print(myString)
コンソールには以下の文字列が出力されます。
Let's trim all the whitespace