strsplit
Split the Elements of a Character Vector

Description

Split strings into sections specified by a regular expression, matching parts by which to split or by parts to keep.

Usage

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

Arguments

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.

Details

For the details of the syntax of the various sorts of regular expression patterns, see regexpr.
If the second argument in split is "" or NULL, x is split into multiple single character-length strings.
Each element of the return value has the same string encoding as the element of x from which it was derived.
Value
returns a list the length of x, where each element is a vector of character strings representing the strings that were split.
See Also
regexpr, Encoding.
Examples
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"

Package base version 6.1.4-13
Package Index