Replace multiple characters, by index, in a string quickly

前端 未结 3 2036
滥情空心
滥情空心 2021-01-25 00:30

I\'m trying to quickly replace multiple characters in a string with another character such as *

For example, I have a string such as:

string         


        
相关标签:
3条回答
  • 2021-01-25 00:58

    We can use substring

    v1 <- c(1, 4, 6, 9)
    for(i in seq_along(v1)) substring(string, v1[i], v1[i]) <- "*"
    #[1] "*bc*e*gh*j"
    

    As we are using stringi, another option is

    library(stringi)
    stri_sub_all(string, from = v1, length = 1) <- "*"
    string
    #[1] "*bc*e*gh*j"
    
    0 讨论(0)
  • 2021-01-25 01:09

    in base R, besides the method of substring() and for-loop shown by @akrun,, you can use utf8ToInt() and intToUtf8 to make it

    v <- utf8ToInt(string)
    v[string_indexes_replaced ] <- utf8ToInt("*")
    res <- intToUtf8(v)
    

    which gives

    > res
    [1] "*bc*e*gh*j"
    
    0 讨论(0)
  • 2021-01-25 01:18

    A simple recursive solution. The time efficiency should be same as iteration (for loop). The benefit is there is no side-effect (assignment of integer ks is localized), so that we can treat its whole computation as a functional abstract and feed it to other part of the bigger program which we are working on. It will help to modularize the code.

    # multi-replace for character vector input with length greater than 1
    multi_replace_v <- function(v, r, ks) {
      ks <- as.integer(ks)
      if (length(ks) == 0) {
        v
      } else if (length(ks) == 1) {
        if (ks[[1]] > length(v) | ks[[1]] < 1) {
          stop("Invalid parameter: ks=", as.character(ks[[1]]), ". Valid range: 1-", as.character(length(v)))
        } else if (ks[[1]] == 1) {
          c(r, v[-1])
        } else if (ks[[1]] == length(v)) {
          c(v[-length(v)], r)
        } else {
          c(v[1:(ks[[1]]-1)], r, v[(ks[[1]]+1):length(v)])
          }
      } else {
        multi_replace_v(multi_replace_v(v, r, ks[[1]]), r, ks[-1])
      }
    }
    
    # multi-replace for input of single string character vector
    multi_replace_s <- function(s, r, ks) paste0(multi_replace_v(unlist(strsplit(s, '')), r, ks), collapse = '') 
    
    # multi-replace for both single string and long vector input
    multi_replace <- function(v_or_s, r, ks) {
      if (length(v_or_s) == 1) {
        multi_replace_s(v_or_s, r, ks)
      } else if (length(v_or_s) > 1) {
        multi_replace_v(v_or_s, r, ks)
      } else {
        NULL
      }
    }
    
    # Example
    > multi_replace('abcdefghij', "*", c(1,4,6,9))
    [1] "*bc*e*gh*j"
    
    0 讨论(0)
提交回复
热议问题