How to remove extra white space between words inside a character vector using?

后端 未结 2 599
半阙折子戏
半阙折子戏 2020-12-08 07:33

Suppose I have a character vector like

\"Hi,  this is a   good  time to   start working   together.\". 

I just want to have



        
相关标签:
2条回答
  • 2020-12-08 08:03

    gsub is your friend:

    test <- "Hi,  this is a   good  time to   start working   together."
    gsub("\\s+"," ",test)
    #[1] "Hi, this is a good time to start working together."
    

    \\s+ will match any space character (space, tab etc), or repeats of space characters, and will replace it with a single space " ".

    0 讨论(0)
  • 2020-12-08 08:08

    Another option is the squish function from the stringr library

    library(stringr)
    string <- "Hi,  this is a   good  time to   start working   together."
    str_squish(string)
    #[1] ""Hi, this is a good time to start working together.""
    
    0 讨论(0)
提交回复
热议问题