Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove all the spaces and \n\r in a String?

Tags:

string

ios

swift

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.

like image 472
Tauta Avatar asked Jan 22 '16 06:01

Tauta


People also ask

How do I remove all spaces from a string in R?

gsub() function is used to remove the space by removing the space in the given string.

How do I remove all spaces from a 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 "".

How do I remove leading spaces in R?

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.

How do I remove spaces from data in R?

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.


1 Answers

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 ;)

like image 136
Eendje Avatar answered Oct 18 '22 05:10

Eendje