What is the most efficient way to remove all the spaces, \n
and \r
in a String in Swift?
I have tried:
for character in string.characters { }
But it's a little inconvenient.
gsub() function is used to remove the space by removing the space in the given string.
The replaceAll() method of the String class replaces each substring of this string that matches the given regular expression with the given replacement. You can remove white spaces from a string by replacing " " with "".
trimws() function in R Language is used to trim the leading white spaces. It shrinks an object by removing outermost rows and columns with the same values.
trimws() function is used to remove or strip, leading and trailing space of the column in R. trimws() function is used to strip leading, trailing and strip all the spaces in R Let's see an example on how to strip leading, trailing and all space of the column in R. Let's first create the dataframe.
Swift 4:
let text = "This \n is a st\tri\rng" let test = String(text.filter { !" \n\t\r".contains($0) })
Output:
print(test) // Thisisastring
While Fahri's answer is nice, I prefer it to be pure Swift ;)
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