I want something like:
"aaaXaaaXaaaXaaaYXaaa".Split('X');
but want it to ignore 'YX'.
Of course I can simply loop and correct for it. But is there a built-in method for that?
To split a string with specific character as delimiter in Java, call split() method on the string object, and pass the specific character as argument to the split() method. The method returns a String Array with the splits as elements in the array.
. split() is a powerful string manipulation tool that we have at our disposal, but it can also be destructive. Splitting will remove delimiters from the returned array, which may give us output that differs greatly from what the original string was.
To split a string by a regular expression, pass a regex as a parameter to the split() method, e.g. str. split(/[,. \s]/) . The split method takes a string or regular expression and splits the string based on the provided separator, into an array of substrings.
You can use a regular expression with a negative lookbehind:
string[] result = Regex.Split(s, "(?<!Y)X");
See it working online: ideone
More information about lookarounds: Lookahead and Lookbehind Zero-Width Assertions
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