strtrim: Shorten Strings to Specified Width¶
Description¶
Right-trims strings so that they do not exceed a given width (as determined by stri_width
).
Usage¶
strtrim(x, width)
Arguments¶
|
character vector whose elements are to be trimmed |
|
numeric vector giving the widths to which the corresponding strings are to be trimmed |
Details¶
Both arguments are recycled if necessary.
Not to be confused with trimws
.
Might be useful when displaying strings using a monospaced font.
Value¶
Returns a character vector (in UTF-8). Preserves object attributes in a similar way as Arithmetic operators.
Differences from Base R¶
Replacement for base strtrim
implemented with (special case of) stri_sprintf
.
both arguments are not recycled in an usual manner [fixed here]
missing values are not allowed in
width
[fixed here]some emojis, combining characters and modifiers (e.g., skin tones) are not recognised properly [fixed here]
attributes are only propagated from the 1st argument [fixed]
See Also¶
The official online manual of stringx at https://stringx.gagolewski.com/
Examples¶
base::strtrim("aaaaa", 1:3)
## [1] "a"
stringx::strtrim("aaaaa", 1:3)
## [1] "a" "aa" "aaa"
x <- c(
"\U0001F4A9",
"\U0001F64D\U0001F3FC\U0000200D\U00002642\U0000FE0F",
"\U0001F64D\U0001F3FB\U0000200D\U00002642",
"\U000026F9\U0001F3FF\U0000200D\U00002640\U0000FE0F",
"\U0001F3F4\U000E0067\U000E0062\U000E0073\U000E0063\U000E0074\U000E007F"
)
print(x)
## [1] "💩" "🙍🏼♂️" "🙍🏻♂" "⛹🏿♀️" "🏴"
base::strtrim(x, 2)
## [1] "💩" "🙍" "🙍" "⛹" "🏴"
stringx::strtrim(x, 2)
## [1] "💩" "🙍🏼♂️" "🙍🏻♂" "⛹🏿♀️" "🏴"