.devel/sphinx/rapi/strtrim.md

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

| | | |----|----| | x | character vector whose elements are to be trimmed | | width | 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.

Author(s)

Marek Gagolewski

See Also

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

Related function(s): sprintf, substr, nchar

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] "💩"    "🙍🏼‍♂️" "🙍🏻‍♂" "⛹🏿‍♀️"  "🏴󠁧󠁢󠁳󠁣󠁴󠁿"


gagolews/stringx documentation built on Jan. 15, 2025, 9:46 p.m.