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

Time:06-02

I have a vector of strings that should each have 8 alphanumeric characters. They might include one or more spaces within the string, not right or left.

For those strings that are less than 8 characters, I'd like to pad the existing spaces, so that in the end all strings have 8 characters. The spaces / padding shall remain within the string. (Background: this is related to UK post codes)

My approach is convoluted and possibly flawed, below an example vector and desired output.

## 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

CodePudding user response:

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

Or even:

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

CodePudding user response:

Try:

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 

CodePudding user response:

We may use sprintf with sub in base R (using R 4.2.0)

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

-output

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

CodePudding user response:

Here is another base R option (but might not be that efficient)

> (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
  • Related