strsplit(x, split, fixed = FALSE, perl = FALSE, useBytes = FALSE)
x | a vector that contains the character strings to split. Only character vectors are allowed. |
split | a regular expression (see regexpr for details) that specifies the rule to apply in spliting the character strings. The parts of the input strings that match this expression are removed and the remaining parts are returned. |
fixed | a logical value. If TRUE, split is treated as a fixed string to use in matching instead of treating it as a regular expression pattern. In this case, no characters are special. The default is FALSE. This argument has priority over perl. |
perl | a logical value. If TRUE, split is treated as a perl regular expression. The default is FALSE. |
useBytes | a logical value. If TRUE, the string is treated as a simple sequence of bytes. If FALSE (the default), and if any of the x or split strings have 'bytes' encoding (see Encoding), then useBytes is set to TRUE and a warning is generated. |
strsplit(c("Hello", "Two words"), "[[:space:]]") # [[1]]: # [1] "Hello" # # [[2]]: # [1] "Two" "words"# Find all numbers in a string strsplit("1.2, 0, .1, -.1e2, 2.3e4, and 2e-5", " *,( and)? *") # [[1]] # [1] "1.2" "0" ".1" "-.1e2" "2.3e4" "2e-5"
# Reverse a string paste(rev(strsplit("Hello, world!", "")[[1]]), collapse="") # [1] "!dlrow ,olleH"
# Use fixed=TRUE to avoid "." maching anything strsplit("one.two.three", ".", fixed=TRUE) # [[1]] # [1] "one" "two" "three"
# Need to escape special characters such as ".", "[", "(" etc... strsplit("a[b[c]]", "\\[|(\\]+)") # [[1]] # [1] "a" "b" "c"
strsplit("abcde", "") # [[1]] # [1] "a" "b" "c" "d" "e"