stringi (version 0.2-5)

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

Description

These functions may be used e.g. to get rid of unnecessary whitespaces 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
character vector
pattern
character vector specifying character classes that should be preserved, see stringi-search-charclass, defaults to `\P{Wspace}`
side
character [stri_trim only]; defaults to "both"

Value

  • All these functions return a character vector.

Details

Vectorized over str and pattern.

stri_trim is a convenience function, which dispatches control to stri_trim_*. Unless you are very lazy, do not use it: it is a little bit slower.

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

Interestingly, with these functions you may sometimes extract data, which in some cases require using regular expressions. E.g. you may get "23.5" out of "total of 23.5 bitcoins".

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

See Also

Other search_charclass: stri_count_charclass; stri_detect_charclass; stri_extract_all_charclass, stri_extract_first_charclass, stri_extract_first_charclass, stri_extract_last_charclass, stri_extract_last_charclass; stri_locate_all_charclass, stri_locate_first_charclass, stri_locate_first_charclass, stri_locate_last_charclass, stri_locate_last_charclass; stri_replace_all_charclass, stri_replace_first_charclass, stri_replace_first_charclass, stri_replace_last_charclass, stri_replace_last_charclass; stri_split_charclass; stringi-search-charclass; stringi-search

Examples

Run this code
if (stri_install_check(silent=TRUE))
stri_trim_left("aaa")

if (stri_install_check(silent=TRUE))
stri_trim_right("rexamine.com/", "\\p{P}")

if (stri_install_check(silent=TRUE))
stri_trim_both("Total of 23.5 bitcoins. ", "\\p{N}")

if (stri_install_check(silent=TRUE))
stri_trim_both("Total of 23.5 bitcoins. ", "\\p{L}")

Run the code above in your browser using DataLab