ID EN
String Functions

trimws

R Base 3.6.2 🇮🇩 Bahasa Indonesia

Hapus spasi awal dan/atau akhir dari string karakter.

Syntax

R
trimws(x, which = c("both", "left", "right"), whitespace = "[ \t\r\n]")

Arguments

Parameter Deskripsi
x a character vector
which a character string specifying whether to remove both leading and trailing whitespace (default), or only leading ("left") or trailing ("right"). Can be abbreviated.
whitespace a string specifying a regular expression to match (one character of) “white space”, see Details for alternatives to the default.

Details

Secara internal, sub(re, "", *, perl = TRUE), yaitu ekspresi reguler perpustakaan PCRE digunakan. Untuk portabilitas, 'spasi' default adalah kelas karakter [ \t\r\n] (spasi, tab horizontal, gerbong kembali, baris baru). Alternatifnya, [\h\v] adalah generalisasi (PCRE) yang bagus untuk mencocokkan semua karakter spasi horizontal dan vertikal Unicode, lihat juga https://www.pcre.org.

Contoh

Example
R
# NOT RUN {
x <- "  Some text. "
x
trimws(x)
trimws(x, "l")
trimws(x, "r")

## Unicode --> need "stronger" 'whitespace' to match all :
tt <- "text with unicode 'non breakable space'."
xu <- paste(" \t\v", tt, "\u00a0 \n\r")
(tu <- trimws(xu, whitespace = "[\\h\\v]"))
stopifnot(identical(tu, tt))
# }