Skip to contents

Split the elements of a character vector into a matrix

Usage

strSplit(x, split, fixed = TRUE, n = Inf)

Arguments

x

character. Character vector that does not contain NA or empty strings.

split

character(1). String that defines the split boundary. The number of characters (nchar) can be greater than 1 here. Regular expressions are intentionally not supported to keep this simple.

fixed

logical(1). If TRUE, pattern is a string to be matched as is. Otherwise, will match by regular expression.

n

Inf or integer(1). Maximum number of strings to return. If Inf, return all strings defined by split boundary. When set, function will split up to this number.

Value

matrix. Character matrix split into columns.

Note

Updated 2023-09-22.

Examples

## Infinite number of fixed splits.
x <- c("a__b__c", "d__e__f", "g__h__i")
mat <- strSplit(x = x, split = "__", fixed = TRUE, n = Inf)
print(mat)
#>      [,1] [,2] [,3]
#> [1,] "a"  "b"  "c" 
#> [2,] "d"  "e"  "f" 
#> [3,] "g"  "h"  "i" 

## Infinite number of regex splits.
x <- c("a_b_c_d", "e_f__g___h", "i__j__k__l")
mat <- strSplit(x = x, split = "_+", fixed = FALSE, n = Inf)
print(mat)
#>      [,1] [,2] [,3] [,4]
#> [1,] "a"  "b"  "c"  "d" 
#> [2,] "e"  "f"  "g"  "h" 
#> [3,] "i"  "j"  "k"  "l" 

## Finite number of fixed splits.
x <- c("a__b__c", "d__e__f", "g__h__i")
mat <- strSplit(x = x, split = "__", fixed = TRUE, n = 2L)
print(mat)
#>      [,1] [,2]  
#> [1,] "a"  "b__c"
#> [2,] "d"  "e__f"
#> [3,] "g"  "h__i"

## Finite number of regex splits.
x <- c("a_b_c_d", "e_f__g___h", "i__j__k__l")
mat <- strSplit(x = x, split = "_+", fixed = FALSE, n = 2L)
print(mat)
#>      [,1] [,2]      
#> [1,] "a"  "b_c_d"   
#> [2,] "e"  "f__g___h"
#> [3,] "i"  "j__k__l"