How to convert a vector of strings to Title Case

前端 未结 5 1223
长情又很酷
长情又很酷 2021-01-11 19:00

I have a vector of strings in lower case. I\'d like to change them to title case, meaning the first letter of every word would be capitalized. I\'ve managed to do it with a

5条回答
  •  北海茫月
    2021-01-11 19:15

    I'll throw one more into the mix for fun:

    topropper(strings)
    [1] "First Phrase"              "Another Phrase To Convert" "And Here's Another One"   
    [4] "Last-one"  
    
    topropper <- function(x) {
      # Makes Proper Capitalization out of a string or collection of strings. 
      sapply(x, function(strn)
       { s <- strsplit(strn, "\\s")[[1]]
           paste0(toupper(substring(s, 1,1)), 
                 tolower(substring(s, 2)),
                 collapse=" ")}, USE.NAMES=FALSE)
    }
    

提交回复
热议问题