获取和删除字符串的第一个字符


102

我想通过为每个字符分配不同的值来使用字符字符串进行二维漫游。我打算“弹出”字符串的第一个字符,使用它,然后对其余字符串重复此操作。

我怎样才能实现这样的目标?

x <- 'hello stackoverflow'

我希望能够执行以下操作:

a <- x.pop[1]

print(a)

'h'
print(x)

'ello stackoverflow'

Answers:


167

请参阅?substring

x <- 'hello stackoverflow'
substring(x, 1, 1)
## [1] "h"
substring(x, 2)
## [1] "ello stackoverflow"

具有pop既返回值又具有更新存储在其中的数据的副作用的方法的想法在x很大程度上是面向对象编程的概念。因此pop,我们可以定义带有方法的引用类,而不是定义对字符向量进行操作的函数pop

PopStringFactory <- setRefClass(
  "PopString",
  fields = list(
    x = "character"  
  ),
  methods = list(
    initialize = function(x)
    {
      x <<- x
    },
    pop = function(n = 1)
    {
      if(nchar(x) == 0)
      {
        warning("Nothing to pop.")
        return("")
      }
      first <- substring(x, 1, n)
      x <<- substring(x, n + 1)
      first
    }
  )
)

x <- PopStringFactory$new("hello stackoverflow")
x
## Reference class object of class "PopString"
## Field "x":
## [1] "hello stackoverflow"
replicate(nchar(x$x), x$pop())
## [1] "h" "e" "l" "l" "o" " " "s" "t" "a" "c" "k" "o" "v" "e" "r" "f" "l" "o" "w"

15

也有str_sub来自纵梁包

x <- 'hello stackoverflow'
str_sub(x, 2) # or
str_sub(x, 2, str_length(x))
[1] "ello stackoverflow"

10

stringi包使用此功能

> x <- 'hello stackoverflow'
> stri_sub(x,2)
[1] "ello stackoverflow"

8

substring绝对是最好的选择,但这是一种strsplit选择,因为我还没有看到。

> x <- 'hello stackoverflow'
> strsplit(x, '')[[1]][1]
## [1] "h"

或同等

> unlist(strsplit(x, ''))[1]
## [1] "h"

您可以paste将其余的字符串放在一起。

> paste0(strsplit(x, '')[[1]][-1], collapse = '')
## [1] "ello stackoverflow"

5

删除第一个字符:

x <- 'hello stackoverflow'
substring(x, 2, nchar(x))

想法是选择所有字符,从2到x中的字符数。当单词或短语中的字符数不相等时,这一点很重要。

选择第一个字母很简单,就像以前的答案一样:

substring(x,1,1)

2

另一种选择是将捕获子表达式与正则表达式函数regmatches和一起使用regexec

# the original example
x <- 'hello stackoverflow'

# grab the substrings
myStrings <- regmatches(x, regexec('(^.)(.*)', x))

这将返回整个字符串,第一个字符,并在长度为1的列表中显示“弹出”结果。

myStrings
[[1]]
[1] "hello stackoverflow" "h"                   "ello stackoverflow" 

等同于list(c(x, substr(x, 1, 1), substr(x, 2, nchar(x))))。也就是说,它包含所需元素的超集以及完整字符串。


加法sapply将使该方法适用于长度> 1的字符向量。

# a slightly more interesting example
xx <- c('hello stackoverflow', 'right back', 'at yah')

# grab the substrings
myStrings <- regmatches(x, regexec('(^.)(.*)', xx))

这将返回一个列表,其中匹配的完整字符串作为第一个元素,而匹配的子表达式()作为以下元素捕获。因此,在正则表达式中'(^.)(.*)'(^.)匹配第一个字符,然后(.*)匹配其余字符。

myStrings
[[1]]
[1] "hello stackoverflow" "h"                   "ello stackoverflow" 

[[2]]
[1] "right back" "r"          "ight back" 

[[3]]
[1] "at yah" "a"      "t yah" 

现在,我们可以使用trusty sapply+ [方法提取所需的子字符串。

myFirstStrings <- sapply(myStrings, "[", 2)
myFirstStrings
[1] "h" "r" "a"
mySecondStrings <- sapply(myStrings, "[", 3)
mySecondStrings
[1] "ello stackoverflow" "ight back"          "t yah"

这是一个很好的技巧,但我认为这是个问题。
pedrosaurio

您需要进一步解释,因为它可以产生与其他答案相同的输出。请参阅sapply用于提取的最后代码块。如问题中所述,“弹出”第一个字符是对所得向量(mySecondStrings)重复此过程的问题。
lmo

当然,它可以与您刚刚添加的额外说明一起使用,但我仍然发现它比应有的更复杂。
pedrosaurio
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.