Suppose I have a long string:
假设我有一个长字符串:
"XOVEWVJIEWNIGOIWENVOIWEWVWEW"
How do I split this to get every 5 characters followed by a space?
如何分割这个以获得每5个字符后跟一个空格?
"XOVEW VJIEW NIGOI WENVO IWEWV WEW"
Note that the last one is shorter.
请注意,最后一个更短。
I can do a loop where I constantly count and build a new string character by character but surely there must be something better no?
我可以做一个循环,我不断计算并按字符构建一个新的字符串字符,但肯定必须有更好的东西吗?
5 个解决方案
#1
43
Using regular expressions:
使用正则表达式:
gsub("(.{5})", "\\1 ", "XOVEWVJIEWNIGOIWENVOIWEWVWEW")
# [1] "XOVEW VJIEW NIGOI WENVO IWEWV WEW"
#2
13
Using sapply
使用sapply
> string <- "XOVEWVJIEWNIGOIWENVOIWEWVWEW"
> sapply(seq(from=1, to=nchar(string), by=5), function(i) substr(string, i, i+4))
[1] "XOVEW" "VJIEW" "NIGOI" "WENVO" "IWEWV" "WEW"
#3
9
You can try something like the following:
您可以尝试以下内容:
s <- "XOVEWVJIEWNIGOIWENVOIWEWVWEW" # Original string
l <- seq(from=5, to=nchar(s), by=5) # Calculate the location where to chop
# Add sentinels 0 (beginning of string) and nchar(s) (end of string)
# and take substrings. (Thanks to @flodel for the condense expression)
mapply(substr, list(s), c(0, l) + 1, c(l, nchar(s)))
Output:
输出:
[1] "XOVEW" "VJIEW" "NIGOI" "WENVO" "IWEWV" "WEW"
Now you can paste
the resulting vector (with collapse=' '
) to obtain a single string with spaces.
现在,您可以粘贴生成的向量(使用collapse ='')以获取带空格的单个字符串。
#4
6
You can also use a sub-string without a loop. substring
is the vectorized substr
您也可以使用没有循环的子字符串。 substring是向量化的substr
x <- "XOVEWVJIEWNIGOIWENVOIWEWVWEW"
n <- seq(1, nc <- nchar(x), by = 5)
paste(substring(x, n, c(n[-1]-1, nc)), collapse = " ")
# [1] "XOVEW VJIEW NIGOI WENVO IWEWV WEW"
#5
5
No *apply stringi
solution:
否*应用stringi解决方案:
x <- "XOVEWVJIEWNIGOIWENVOIWEWVWEW"
stri_sub(x, seq(1, stri_length(x),by=5), length=5)
[1] "XOVEW" "VJIEW" "NIGOI" "WENVO" "IWEWV" "WEW"
This extracts substrings just like in @Jilber answer, but stri_sub
function is vectorized se we don't need to use *apply here.
这提取子串就像在@Jilber中一样,但是stri_sub函数是矢量化的,我们不需要使用* apply here。
#1
43
Using regular expressions:
使用正则表达式:
gsub("(.{5})", "\\1 ", "XOVEWVJIEWNIGOIWENVOIWEWVWEW")
# [1] "XOVEW VJIEW NIGOI WENVO IWEWV WEW"
#2
13
Using sapply
使用sapply
> string <- "XOVEWVJIEWNIGOIWENVOIWEWVWEW"
> sapply(seq(from=1, to=nchar(string), by=5), function(i) substr(string, i, i+4))
[1] "XOVEW" "VJIEW" "NIGOI" "WENVO" "IWEWV" "WEW"
#3
9
You can try something like the following:
您可以尝试以下内容:
s <- "XOVEWVJIEWNIGOIWENVOIWEWVWEW" # Original string
l <- seq(from=5, to=nchar(s), by=5) # Calculate the location where to chop
# Add sentinels 0 (beginning of string) and nchar(s) (end of string)
# and take substrings. (Thanks to @flodel for the condense expression)
mapply(substr, list(s), c(0, l) + 1, c(l, nchar(s)))
Output:
输出:
[1] "XOVEW" "VJIEW" "NIGOI" "WENVO" "IWEWV" "WEW"
Now you can paste
the resulting vector (with collapse=' '
) to obtain a single string with spaces.
现在,您可以粘贴生成的向量(使用collapse ='')以获取带空格的单个字符串。
#4
6
You can also use a sub-string without a loop. substring
is the vectorized substr
您也可以使用没有循环的子字符串。 substring是向量化的substr
x <- "XOVEWVJIEWNIGOIWENVOIWEWVWEW"
n <- seq(1, nc <- nchar(x), by = 5)
paste(substring(x, n, c(n[-1]-1, nc)), collapse = " ")
# [1] "XOVEW VJIEW NIGOI WENVO IWEWV WEW"
#5
5
No *apply stringi
solution:
否*应用stringi解决方案:
x <- "XOVEWVJIEWNIGOIWENVOIWEWVWEW"
stri_sub(x, seq(1, stri_length(x),by=5), length=5)
[1] "XOVEW" "VJIEW" "NIGOI" "WENVO" "IWEWV" "WEW"
This extracts substrings just like in @Jilber answer, but stri_sub
function is vectorized se we don't need to use *apply here.
这提取子串就像在@Jilber中一样,但是stri_sub函数是矢量化的,我们不需要使用* apply here。