Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

stri_trim

Trim Characters from the Left and/or Right Side of a String


Description

These functions may be used, e.g., to remove unnecessary white-spaces from strings. Trimming ends at the first or starts at the last pattern match.

Usage

stri_trim_both(str, pattern = "\\P{Wspace}")

stri_trim_left(str, pattern = "\\P{Wspace}")

stri_trim_right(str, pattern = "\\P{Wspace}")

stri_trim(str, side = c("both", "left", "right"), pattern = "\\P{Wspace}")

Arguments

str

a character vector of strings to be trimmed

pattern

a single pattern, specifying character classes that should be preserved (see stringi-search-charclass). Defaults to '\P{Wspace}.

side

character [stri_trim only]; defaults to 'both'

Details

Vectorized over str and pattern.

stri_trim is a convenience wrapper over stri_trim_left and stri_trim_right.

Contrary to many other string processing libraries, our trimming functions are universal. A character class, given by pattern, may be adjusted to suit your needs (yet, most often you stick to the default value).

For replacing pattern matches with arbitrary replacement string, see stri_replace.

Trimming can also be used where you would normally rely on regular expressions. For instance, you may get '23.5' out of 'total of 23.5 bitcoins'.

For trimming white-spaces, please note the difference between Unicode binary property '\p{Wspace}' (more universal) and general character category '\p{Z}', see stringi-search-charclass.

Value

All these functions return a character vector.

See Also

Other search_replace: about_search, stri_replace_all()

Other search_charclass: about_search_charclass, about_search

Examples

stri_trim_left('               aaa')
stri_trim_right('r-project.org/', '\\p{P}')
stri_trim_both(' Total of 23.5 bitcoins. ', '\\p{N}')
stri_trim_both(' Total of 23.5 bitcoins. ', '\\p{L}')

stringi

Character String Processing Facilities

v1.6.1
file LICENSE
Authors
Marek Gagolewski [aut, cre, cph] (<https://orcid.org/0000-0003-0637-6028>), Bartek Tartanus [ctb], and others (stringi source code); IBM, Unicode, Inc. and others (ICU4C source code, Unicode Character Database)
Initial release
2021-05-05

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.