Alternative function to paste

前端 未结 5 1915
陌清茗
陌清茗 2021-02-04 03:43

Is there a function that can be an alternative to paste ? I would like to know if something like this exists in R:

> buildString ( \"Hi {1}, Have a very nice         


        
5条回答
  •  广开言路
    2021-02-04 03:54

    frankc and DWin are right to point you to sprintf().

    If for some reason your replacement parts really will be in the form of a vector (i.e. c("Tom", "day")), you can use do.call() to pass them in to sprintf():

    string <- "Hi %s, Have a really nice %s!"
    vals   <- c("Tom", "day")
    
    do.call(sprintf, as.list(c(string, vals)))
    # [1] "Hi Tom, Have a really nice day!"
    

提交回复
热议问题