Truncates one or more strings to a specified length, adding an ellipsis
(...) to those strings that have been truncated. The truncation can also be
performed using word boundaries. Use strAlign() to justify
the strings if needed.
Arguments
- x
a vector of strings
- maxlen
the maximum length of the returned strings (NOT counting the appended ellipsis).
maxlenis recycled.- ellipsis
the string to be appended, if the string is longer than the given maximal length. The default is
"...".- wbound
logical. Determines if the maximal length should be reduced to the next smaller word boundary and so words are not chopped. Default is
FALSE.
See also
string-overview for an overview of all string utilities in pharos.
Examples
x <- c("this is short", "and this is a longer text",
"whereas this is a much longer story, which could not be told shorter")
# simple truncation on 10 characters
strTrunc(x, maxlen=10)
#> [1] "this is sh..." "and this i..." "whereas th..."
# NAs remain NA
strTrunc(c(x, NA_character_), maxlen=15, wbound=TRUE)
#> [1] "this is short" "and this is a..." "whereas this is..."
#> [4] NA
# using word boundaries
for(i in 0:20)
print(strTrunc(x, maxlen=i, wbound=TRUE))
#> [1] "..." "..." "..."
#> [1] "..." "..." "..."
#> [1] "..." "..." "..."
#> [1] "..." "and..." "..."
#> [1] "this..." "and..." "..."
#> [1] "this..." "and..." "..."
#> [1] "this..." "and..." "..."
#> [1] "this is..." "and..." "whereas..."
#> [1] "this is..." "and this..." "whereas..."
#> [1] "this is..." "and this..." "whereas..."
#> [1] "this is..." "and this..." "whereas..."
#> [1] "this is..." "and this is..." "whereas..."
#> [1] "this is..." "and this is..." "whereas this..."
#> [1] "this is short" "and this is a..." "whereas this..."
#> [1] "this is short" "and this is a..." "whereas this..."
#> [1] "this is short" "and this is a..." "whereas this is..."
#> [1] "this is short" "and this is a..." "whereas this is..."
#> [1] "this is short" "and this is a..." "whereas this is a..."
#> [1] "this is short" "and this is a..." "whereas this is a..."
#> [1] "this is short" "and this is a..." "whereas this is a..."
#> [1] "this is short" "and this is a longer..."
#> [3] "whereas this is a..."
# compare
for(i in 0:20)
print(strTrunc(x, maxlen=i, wbound=FALSE))
#> [1] "..." "..." "..."
#> [1] "t..." "a..." "w..."
#> [1] "th..." "an..." "wh..."
#> [1] "thi..." "and..." "whe..."
#> [1] "this..." "and ..." "wher..."
#> [1] "this ..." "and t..." "where..."
#> [1] "this i..." "and th..." "wherea..."
#> [1] "this is..." "and thi..." "whereas..."
#> [1] "this is ..." "and this..." "whereas ..."
#> [1] "this is s..." "and this ..." "whereas t..."
#> [1] "this is sh..." "and this i..." "whereas th..."
#> [1] "this is sho..." "and this is..." "whereas thi..."
#> [1] "this is shor..." "and this is ..." "whereas this..."
#> [1] "this is short" "and this is a..." "whereas this ..."
#> [1] "this is short" "and this is a ..." "whereas this i..."
#> [1] "this is short" "and this is a l..." "whereas this is..."
#> [1] "this is short" "and this is a lo..." "whereas this is ..."
#> [1] "this is short" "and this is a lon..." "whereas this is a..."
#> [1] "this is short" "and this is a long..." "whereas this is a ..."
#> [1] "this is short" "and this is a longe..." "whereas this is a m..."
#> [1] "this is short" "and this is a longer..."
#> [3] "whereas this is a mu..."
