Home > database >  Removing \r, \n and \t, from a string
Removing \r, \n and \t, from a string

Time:01-13

I have one string which looks like this :

"Způsob využití:\r\n\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\tobjekt k bydlení\r\n\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t"

I am confused that how i suppose to clean it? i want to remove everything which looks like :

\r, \n, \t.

my output should be Způsob využití: objekt k bydlení

any help how i can achieve it? i am new to r and dont know much of it

CodePudding user response:

You can use stringr::str_squish():

stringr::str_squish(x)

# [1] "Způsob využití: objekt k bydlení"

It can be achieved with base:

trimws(gsub('\\s ', ' ', x))

CodePudding user response:

stringr::str_remove_all(x, "\\t|\\n|\\r")

is answer thanks to @user438383

CodePudding user response:

gsub("\\n|\\t|\\r", "", "Způsob využití:\r\n\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\tobjekt k bydlení\r\n\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t")
[1] "Způsob využití:objekt k bydlení"

CodePudding user response:

U can try -

re.sub(r"\\n\\n", " ",string) 
  • Related