Reputation: 177
I want to remove the extra spaces between the top , bottom and in between the text. I've tried with this one, but it's removed all the new lines.
let newString = textView.text.replacingOccurrences(of: "\n", with: "")
Result - hellohow are youhow may i help you
Actual input:
hello
how are you
how may i help you
Expected Result:
hello
how are you
how may i help you
Thanks!
Upvotes: 0
Views: 1458
Reputation: 137
You can try this :
let new = yourTextView.text.trimmingCharacters(in : .whitespacesAndNewlines)
Upvotes: 0
Reputation: 82759
Method stringByTrimmingCharactersInSet returns a new string made by removing from both ends of the String characters contained in a given character set.
We can also just remove only whitespace, newline or both.
let newString = textView.text.trimmingCharacters(in: .whitespacesAndNewlines)
Upvotes: 1