walle_eva
walle_eva

Reputation: 81

Removing \r, \n and \t, from a string

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

Upvotes: 1

Views: 921

Answers (3)

walle_eva
walle_eva

Reputation: 81

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

is answer thanks to @user438383

Upvotes: 1

Darren Tsai
Darren Tsai

Reputation: 35554

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

Upvotes: 4

juanbarq
juanbarq

Reputation: 399

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í"

Upvotes: 0

Related Questions