ID EN
String Functions

strsplit

R Base 3.6.2 🇮🇩 Bahasa Indonesia

Pisahkan elemen vektor karakter x menjadi substring sesuai dengan kecocokan dengan pemisahan substring di dalamnya.

Syntax

R
strsplit(x, split, fixed = FALSE, perl = FALSE, useBytes = FALSE)

Arguments

Parameter Deskripsi
x character vector, each element of which is to be split. Other inputs, including a factor, will give an error.
split character vector (or object which can be coerced to such) containing regular expression(s) (unless fixed = TRUE) to use for splitting. If empty matches occur, in particular if split has length 0, x is split into single characters. If split has length greater than 1, it is re-cycled along x.
fixed logical. If TRUE match split exactly, otherwise use regular expressions. Has priority over perl.
perl logical. Should Perl-compatible regexps be used?
useBytes logical. If TRUE the matching is done byte-by-byte rather than character-by-character, and inputs with marked encodings are not converted. This is forced (with a warning) if any input is found which is marked as "bytes" (see Encoding).

Return Value

Daftar yang panjangnya sama dengan x, elemen ke-i berisi vektor pembagian x[i]. Jika ada elemen x atau split yang dideklarasikan dalam UTF-8 (lihat Pengkodean), semua string karakter non-ASCII dalam hasilnya akan dalam UTF-8 dan pengkodeannya dinyatakan sebagai UTF-8. Untuk perl = TRUE, useBytes = FALSE semua string non-ASCII dalam lokal multibyte diterjemahkan ke UTF-8.

Details

Pemisahan argumen akan dipaksakan ke karakter, jadi Anda akan melihat penggunaan split = NULL yang berarti split = karakter(0), termasuk contoh di bawah. Perhatikan bahwa pemisahan menjadi satu karakter dapat dilakukan melalui split = character(0) atau split = ""; keduanya setara. Definisi 'karakter' di sini bergantung pada lokal: dalam lokal byte tunggal itu adalah byte, dan dalam lokal multi-byte itu adalah unit yang diwakili oleh 'karakter lebar' (hampir selalu berupa titik kode Unicode). Nilai split yang hilang tidak membagi elemen x yang bersesuaian sama sekali. Algoritma yang diterapkan pada setiap string masukan adalah repeat { jika string kosong istirahat. jika ada kecocokan, tambahkan string di sebelah kiri kecocokan ke output. hapus korek api dan semua yang ada di sebelah kirinya. jika tidak, tambahkan string ke output. Bre

Contoh

Example
R
# NOT RUN {
noquote(strsplit("A text I want to display with spaces", NULL)[[1]])

x <- c(as = "asfef", qu = "qwerty", "yuiop[", "b", "stuff.blah.yech")
# split x on the letter e
strsplit(x, "e")

unlist(strsplit("a.b.c", "."))
## [1] "" "" "" "" ""
## Note that 'split' is a regexp!
## If you really want to split on '.', use
unlist(strsplit("a.b.c", "[.]"))
## [1] "a" "b" "c"
## or
unlist(strsplit("a.b.c", ".", fixed = TRUE))

## a useful function: rev() for strings
strReverse <- function(x)
        sapply(lapply(strsplit(x, NULL), rev), paste, collapse = "")
strReverse(c("abc", "Statistics"))

## get the first names of the members of R-core
a <- readLines(file.path(R.home("doc"),"AUTHORS"))[-(1:8)]
a <- a[(0:2)-length(a)]
(a <- sub(" .*","", a))
# and reverse them
strReverse(a)

## Note that final empty strings are not produced:
strsplit(paste(c("", "a", ""), collapse="#"), split="#")[[1]]
# [1] ""  "a"
## and also an empty string is only produced before a definite match:
strsplit("", " ")[[1]]    # character(0)
strsplit(" ", " ")[[1]]   # [1] ""
# }

See Also

paste for the reverse grep and sub for string search and manipulation; also nchar substr. ‘regular expression’ for the details of the pattern specification. Option PCRE_use_JIT controls the details when perl = TRUE.