删除字符串中第一个“反斜杠"之后的所有内容 [英] removing everything after first 'backslash' in a string

查看:311
本文介绍了删除字符串中第一个“反斜杠"之后的所有内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像下面的向量

vec <- c("abc\edw\www", "nmn\ggg", "rer\qqq\fdf"......)

我想在遇到第一个斜杠后立即删除所有内容,如下所示

I want to remove everything after as soon as first slash is encountered, like below

newvec <- c("abc","nmn","rer")

谢谢.

我的原始向量如下(仅头部)

My original vector is as below (only the head)

[1] "peoria ave\nste \npeoria"                      [2]   "wood dr\nphoenix"                                  
"central ave\nphoenix"                            
[4] "southern ave\nphoenix"                         [5]   "happy valley rd\nste   
\nglendaleaz "               "the americana at brand\n americana way\nglendale"

这里的问题是我的原始csv文件不包含反斜杠,但是当我阅读它时,会出现反斜杠.原始csv文件如下

Here the problem is my original csv file does not contain backslashes, but when i read it backslashes appear. Original csv file is as below

[1] "peoria ave               [2] "wood dr
     nste                          nphoenix"       
     npeoria"

如您所见,它们实际上由"ENTER"分隔,但是当我使用read.csv()在R中读取它时,它们会被反斜杠替换.

As you can see, they are actually separated by "ENTER" but when i read it in R using read.csv() they are replaced by backslashes.

推荐答案

另一种解决方案:

 sub("\\\\.*", "", x)

这篇关于删除字符串中第一个“反斜杠"之后的所有内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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