x<-c("apple", "banana", "pear","why")# 连接str_c(x, collapse ="; ")#> [1] "apple; banana; pear; why"paste0("Hello ", c("John", "Susan"))#> [1] "Hello John" "Hello Susan"paste("x",c("a","b"),sep="",collapse ="?")#> [1] "xa?xb"paste0("x",c("A","B"),collapse="?")#> [1] "xA?xB"cat("hello","BOb","\b\n","\bIsn\' R","\t","GREAT?\n",sep =" ")#> hello BOb #> Isn' R GREAT?# str_length(x)#> [1] 5 6 4 3str_sub(x,start =1,end =2)#> [1] "ap" "ba" "pe" "wh"str_dup(x, times =2)#> [1] "appleapple" "bananabanana" "pearpear" "whywhy"# 空格str_pad(x, 10, "both")#> [1] " apple " " banana " " pear " " why "x<-c(" a ", "b ", " c")str_trim(x,side ="left")#> [1] "a " "b " "c"jabberwocky<-str_c("`Twas brillig, and the slithy toves ","did gyre and gimble in the wabe: ","All mimsy were the borogoves, ","and the mome raths outgrabe. ")str_wrap(jabberwocky, width =40)#> [1] "`Twas brillig, and the slithy toves did\ngyre and gimble in the wabe: All mimsy\nwere the borogoves, and the mome raths\noutgrabe."cat(str_wrap(jabberwocky, width =40))#> `Twas brillig, and the slithy toves did#> gyre and gimble in the wabe: All mimsy#> were the borogoves, and the mome raths#> outgrabe.# 截断x<-"This string is moderately long"rbind(str_trunc(x, 20, "right"),str_trunc(x, 20, "left"),str_trunc(x, 20, "center"))#> [,1] #> [1,] "This string is mo..."#> [2,] "...s moderately long"#> [3,] "This stri...ely long"# Locale sensitive x<-"I like horses."str_to_upper(x)#> [1] "I LIKE HORSES."str_to_title(x)#> [1] "I Like Horses."str_to_lower(x)#> [1] "i like horses."str_to_lower(x,locale ="tr")#> [1] "ı like horses."# 排序x<-c("y", "i", "k")str_order(x)#> [1] 2 3 1str_sort(x,locale ="en")#> [1] "i" "k" "y"
name<-"Fred"age<-50anniversary<-as.Date("1991-10-12")str_glue("My name is {name}, ","my age next year is {age + 1}, ","and my anniversary is {format(anniversary, '%A, %B %d, %Y')}.")#> My name is Fred, my age next year is 51, and my anniversary is 星期六, 十月 12, 1991.str_glue("My name is {name}, not {{name}}.")#> My name is Fred, not {name}.