#### Find the number of spaces in a string

``````ANs 3
Column1 Column2 Column3 Column14
I          am     going    out
``````
4 个回复

``````> read.table(textConnection("I am going Out"))
V1 V2    V3  V4
1  I am going Out
``````

``````length(gregexpr(" ", "I am going Out")[[1]])
``````

`[[1]]`获取结果列表的第一个元素，它对应于输入向量的第一个项目，其中“我要外出”作为其唯一元素。 如果你在那里传递了一个不同的向量，你的列表可能有多个元素，或者根本没有一个空向量。

``````countSpaces <- function(s) { sapply(gregexpr(" ", s), function(p) { sum(p>=0) } ) }
``````

``````foo <- transform(foo, baz = countSpaces(bar))
``````

``````R> strsplit("I am going Out", " ")[[1]]
[1] "I"     "am"    "going" "Out"
``````

``````R> length(strsplit("I am going Out", " ")[[1]])
[1] 4
``````

``````x <- "I am going Out"
nchar(x)- nchar(gsub(" ", "", x))
``````

``````as.data.frame(matrix(unlist(strsplit("I am   going Out", "\\s+", perl=TRUE)), nrow=1))
``````

``````library(stringr)
text = "I am going Out"
#matches regular expression
str_count(text, ' ')
``````

``````#matches literal text
str_count(text, fixed(' '))
``````
1 查找字符串中的字符数和数字

4 在字符串中查找字符

2015-02-08 14:41:18 3 42   string
6 在字符串递归算法中查找字符数

8 在列表中查找字符串

2014-01-29 05:19:37 2 111   racket
9 在PHP中查找字符串

2014-10-13 17:48:48 3 56   php
10 查找字符串中的日期

2014-11-09 18:35:40 1 148   java