Learn R Programming

stringx (version 0.2.9)

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)

Value

Returns a character vector (in UTF-8). Preserves object attributes in a similar way as Arithmetic operators.

Arguments

x

character vector whose elements are to be trimmed

width

numeric vector giving the widths to which the corresponding strings are to be trimmed

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]

Details

Both arguments are recycled if necessary.

Not to be confused with trimws.

Might be useful when displaying strings using a monospaced font.

See Also

The official online manual of stringx at https://stringx.gagolewski.com/

Related function(s): sprintf, substr, nchar

Examples

Run this code
base::strtrim("aaaaa", 1:3)
stringx::strtrim("aaaaa", 1:3)
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)
base::strtrim(x, 2)
stringx::strtrim(x, 2)

Run the code above in your browser using DataLab