使用 substr() 删除一个字符
Using substr() to delete a character
我有一个角色"abc"
,想删除"b"
。我想按位置定位。我试过了:
x <- "abc"
substr(x, 2,3) <- ""
x
#[1] "abc"
为什么不能从这样的字符串中删除一个字符?我将如何使用同样简单的方法来做到这一点?
通常,您可以在此处使用 sub/gsub
:
gsub("b", "", x)
但这会删除所有 b
字母,无论它们出现在何处。如果你想定位夹在 a
和 c
之间的 b
,你可以变得聪明并使用 lookarounds:
gsub("(?<=a)b(?=c)", "", x, perl=TRUE)
编辑:
正如@DavidKlotz 在他下面的评论中指出的那样,如果你真的想删除第二个字符,你可以使用:
gsub("(?<=^.).","", x, perl=TRUE)
您应该使用 gsub() 函数:
gsub(pattern = "b",replacement = "",x = x)
你总是可以使用 strsplit
这很有用如果你想按位置解决这个问题(即在你的情况下删除第二个字母),
paste(strsplit(x, '')[[1]][-2], collapse = '')
#[1] "ac"
要应用于多个字符串,我们需要在拆分后进行迭代,即
x <- c('abc', 'sdfre', 'xyz')
sapply(strsplit(x, ''), function(i)paste(i[-2], collapse = ''))
#[1] "ac" "sfre" "xz"
你说你 "want to target by position",如果你的意思是你想从你的字符串中提取第二个字符而不考虑它的值,那么你可以简单地做:
x <- paste0(substr(x, 1, 1), substr(x, 3, nchar(x)))
# "ac" if x <- "abc" initially and "acd" if x <- "abcd" initially
您可以用 substr()
替换字符但不能删除它(因为您需要移动所有后续字符的位置等)。为此,您可以像这样组合 substr
和 gsub
(例如,如果您确定您的字符串不包含破折号):
substr(x, 2, 3) <- '-'
gsub('-', '', x)
如果您想要删除 "b"
次出现,那么 gsub()
是一个不错的选择,如上面的答案所述。
好像不支持空字符串
我认为破解它的最简单方法是使用一个不可能出现在您的数据中的字符,然后 gsub
解决:
x <- "abc"
substr(x, 2,3) <- "\a"
x <- gsub("\a","",x)
x
# [1] "ac"
您当然可以按照其他建议直接使用 gsub
,但我认为用索引替换是一个重要的功能。
\a
是一个铃字,来自维基百科:
A bell code (sometimes bell character) is a device control code
originally sent to ring a small electromechanical bell on tickers and
other teleprinters and teletypewriters to alert operators at the other
end of the line, often of an incoming message
所以你可能是安全的!
我有一个角色"abc"
,想删除"b"
。我想按位置定位。我试过了:
x <- "abc"
substr(x, 2,3) <- ""
x
#[1] "abc"
为什么不能从这样的字符串中删除一个字符?我将如何使用同样简单的方法来做到这一点?
通常,您可以在此处使用 sub/gsub
:
gsub("b", "", x)
但这会删除所有 b
字母,无论它们出现在何处。如果你想定位夹在 a
和 c
之间的 b
,你可以变得聪明并使用 lookarounds:
gsub("(?<=a)b(?=c)", "", x, perl=TRUE)
编辑:
正如@DavidKlotz 在他下面的评论中指出的那样,如果你真的想删除第二个字符,你可以使用:
gsub("(?<=^.).","", x, perl=TRUE)
您应该使用 gsub() 函数:
gsub(pattern = "b",replacement = "",x = x)
你总是可以使用 strsplit
这很有用如果你想按位置解决这个问题(即在你的情况下删除第二个字母),
paste(strsplit(x, '')[[1]][-2], collapse = '')
#[1] "ac"
要应用于多个字符串,我们需要在拆分后进行迭代,即
x <- c('abc', 'sdfre', 'xyz')
sapply(strsplit(x, ''), function(i)paste(i[-2], collapse = ''))
#[1] "ac" "sfre" "xz"
你说你 "want to target by position",如果你的意思是你想从你的字符串中提取第二个字符而不考虑它的值,那么你可以简单地做:
x <- paste0(substr(x, 1, 1), substr(x, 3, nchar(x)))
# "ac" if x <- "abc" initially and "acd" if x <- "abcd" initially
您可以用 substr()
替换字符但不能删除它(因为您需要移动所有后续字符的位置等)。为此,您可以像这样组合 substr
和 gsub
(例如,如果您确定您的字符串不包含破折号):
substr(x, 2, 3) <- '-'
gsub('-', '', x)
如果您想要删除 "b"
次出现,那么 gsub()
是一个不错的选择,如上面的答案所述。
好像不支持空字符串
我认为破解它的最简单方法是使用一个不可能出现在您的数据中的字符,然后 gsub
解决:
x <- "abc"
substr(x, 2,3) <- "\a"
x <- gsub("\a","",x)
x
# [1] "ac"
您当然可以按照其他建议直接使用 gsub
,但我认为用索引替换是一个重要的功能。
\a
是一个铃字,来自维基百科:
A bell code (sometimes bell character) is a device control code originally sent to ring a small electromechanical bell on tickers and other teleprinters and teletypewriters to alert operators at the other end of the line, often of an incoming message
所以你可能是安全的!