-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathclipString.R
22 lines (22 loc) · 864 Bytes
/
clipString.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#' Shortens strings to a given length.
#'
#' @param x [\code{character}]\cr
#' Vector of strings.
#' @param len [\code{integer(1)}]\cr
#' Absolute length the string should be clipped to, including \code{tail}.
#' Note that you cannot clip to a shorter length than \code{tail}.
#' @param tail [\code{character(1)}]\cr
#' If the string has to be shortened at least 1 character, the final characters will be \code{tail}.
#' Default is \dQuote{...}.
#' @return [\code{character(1)}].
#' @export
#' @examples
#' print(clipString("abcdef", 10))
#' print(clipString("abcdef", 5))
clipString = function(x, len, tail = "...") {
assertCharacter(x, any.missing = TRUE)
len = asInteger(len, len = 1L, lower = nchar(tail))
assertString(tail)
ind = (!is.na(x) & nchar(x) > len)
replace(x, ind, paste(substr(x[ind], 1L, len - nchar(tail)), tail, sep = ""))
}