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
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)))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With