Split a string every 5 characters

2019-01-14 11:13发布

Suppose I have a long string:

"XOVEWVJIEWNIGOIWENVOIWEWVWEW"

How do I split this to get every 5 characters followed by a space?

"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条回答
混吃等死
2楼-- · 2019-01-14 11:34

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.

查看更多
何必那么认真
3楼-- · 2019-01-14 11:44

You can also use a sub-string without a loop. substring is the vectorized 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"
查看更多
迷人小祖宗
4楼-- · 2019-01-14 11:47

Using regular expressions:

gsub("(.{5})", "\\1 ", "XOVEWVJIEWNIGOIWENVOIWEWVWEW")
# [1] "XOVEW VJIEW NIGOI WENVO IWEWV WEW"
查看更多
聊天终结者
5楼-- · 2019-01-14 11:51

No *apply stringi solution:

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.

查看更多
你好瞎i
6楼-- · 2019-01-14 11:55

Using 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"  
查看更多
登录 后发表回答