I would like to write a strsplit command that grabs the first ")" and splits the string.
For example:
f("12)34)56") "12" "34)56"
I have read over several other related regex SO questions but I am afraid I am not able to make heads or tails of this. Thank you any assistance.
To split a JavaScript string only on the first occurrence of a character, call the slice() method on the string, passing it the index of the character + 1 as a parameter. The slice method will return the portion of the string after the first occurrence of the character.
Use the str. split() method with maxsplit set to 1 to split a string on the first occurrence, e.g. my_str. split('-', 1) . The split() method only performs a single split when the maxsplit argument is set to 1 .
C = strsplit( str , delimiter ) splits str at the delimiters specified by delimiter . If str has consecutive delimiters, with no other characters between them, then strsplit treats them as one delimiter. For example, both strsplit('Hello,world',',') and strsplit('Hello,,,world',',') return the same output.
maxsplit : It is a number, which tells us to split the string into maximum of provided number of times. If it is not provided then the default is -1 that means there is no limit. Returns : Returns a list of strings after breaking the given string by the specified separator.
You could get the same list-type result as you would with strsplit
if you used regexpr
to get the first match, and then the inverted result of regmatches
.
x <- "12)34)56" regmatches(x, regexpr(")", x), invert = TRUE) # [[1]] # [1] "12" "34)56"
Need speed? Then go for stringi
functions. See timings e.g. here.
library(stringi) x <- "12)34)56" stri_split_fixed(str = x, pattern = ")", n = 2)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With