如何使用字符向量中的单词之间删除多余的空格? [英] How to remove extra white space between words inside a character vector using?

查看:38
本文介绍了如何使用字符向量中的单词之间删除多余的空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个像

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

我只想拥有

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

两个单词之间只有一个空格.我应该如何在 R 中执行此操作?

Only one white space between two words. How should I do this in R?

推荐答案

gsub 是你的朋友:

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+ 将匹配任何空格字符(空格、制表符等)或空格字符的重复,并将其替换为单个空格 " ".

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

这篇关于如何使用字符向量中的单词之间删除多余的空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆