将字符串内部/中间的空格填充到特定数量的字符

padding spaces inside/ in the middle of string to specific number of characters

我有一个字符串向量,每个字符串应该有 8 个字母数字字符。它们可能 字符串中包含一个或多个空格,而不是右边或左边。

对于那些少于8个字符的字符串,我想把已有的空格补上,这样最后所有的字符串都是8个字符。空格/填充应保留在 字符串中。 (背景:这与英国 post 代码有关)

我的方法很复杂,可能有缺陷,下面是一个示例向量和所需的输出。

## x can take any alphanumeric value
x <- c("xxx xxx", "xx xxx", "x  xxx", "xxx  xxx", "xx   xxx", "xxxxxxxx")

## missing number of spaces
s_miss <- 8 - nchar(x)
## numbers of spaces
s_pres <- stringr::str_count(x, "\s")

## now here is a convoluted function
## if a space is found, the missing spaces will be added to the already present spaces
padded <- sapply(1: length(x), function(i){
    gsub("\s+", paste(rep(" ", s_pres[i] + s_miss[i]), collapse = ""), x[i])})

## desired output 
padded
#> [1] "xxx  xxx" "xx   xxx" "x    xxx" "xxx  xxx" "xx   xxx" "xxxxxxxx"
nchar(padded)
#> [1] 8 8 8 8 8 8

尝试:

pa <- sapply(x, \(y) sub(" ", strrep(" ", 9 - nchar(y)), y))
pa
#  xxx xxx     xx xxx     x  xxx   xxx  xxx   xx   xxx   xxxxxxxx 
#"xxx  xxx" "xx   xxx" "x    xxx" "xxx  xxx" "xx   xxx" "xxxxxxxx" 

nchar(pa)
# xxx xxx   xx xxx   x  xxx xxx  xxx xx   xxx xxxxxxxx 
#       8        8        8        8        8        8 
regmatches(x, regexpr(' ', x)) <- strrep(' ', 9 - nchar(x))
x
[1] "xxx  xxx" "xx   xxx" "x    xxx" "xxx  xxx" "xx   xxx" "xxxxxxxx"

甚至:

 stringr::str_replace(x, ' ', strrep(' ', 9 - nchar(x)))

我们可以在base R中使用sprintfsub(使用R 4.2.0

x1 <- sub("^(\s+)(\S+)", "\2\1", sprintf('%8s', x))

-输出

> x1
[1] "xxx  xxx" "xx   xxx" "x    xxx" "xxx  xxx" "xx   xxx" "xxxxxxxx"
> nchar(x1)
[1] 8 8 8 8 8 8

这是另一个基本的 R 选项(但可能效率不高)

> (s <- sapply(strsplit(x, "\s+"), function(x) paste0(x, collapse = strrep(" ", 8 - sum(nchar(x))))))
[1] "xxx  xxx" "xx   xxx" "x    xxx" "xxx  xxx" "xx   xxx" "xxxxxxxx"

> nchar(s)
[1] 8 8 8 8 8 8