R:截断字符串而不分割单词

时间:2012-06-19 12:36:25

标签: string r

我有很多字符串,其中一些很长,就像这样:

movie.titles <- c("Il divo: La spettacolare vita di Giulio Andreotti","Defiance","Coco Before Chanel","Happy-Go-Lucky","Up","The Imaginarium of Doctor Parnassus")

我现在想要将这些字符串截断为最多30个字符,但是这样的方式是在过程中没有单词被分开,理想情况下如果字符串被截断,则将椭圆添加到结尾字符串。

2 个答案:

答案 0 :(得分:4)

这是一个基于R的解决方案:

trimTitles <- function(titles) {
    len <- nchar(titles)
    cuts <- sapply(gregexpr(" ", titles), function(X) {
            max(X[X<27])})
    titles[len>=27] <- paste0(substr(titles[len>=27], 0, cuts[len>=27]), "...")
    titles
}
trimTitles(movie.titles)
# [1] "Il divo: La spettacolare ..."  "Defiance"                     
# [3] "Coco Before Chanel"            "Happy-Go-Lucky"               
# [5] "Up"                            "The Imaginarium of Doctor ..."

答案 1 :(得分:0)

我建议你看看abbreviate功能。它缩写字符串,并允许一些控制。参见:

http://stat.ethz.ch/R-manual/R-devel/library/base/html/abbreviate.html

手册页。