I want to manipulate a String, my String are :
String string = "dfwx--05-dfpixc3"
I want my output to be like :
dfwx--05
dfpixc3
So the separator is - and not --
moreover, can I solve this problem using Matcher class Pattern class ?
You can use split with this regex \b-\b with Word Boundaries like so :
\brepresents an anchor like caret (it is similar to$and^) matching positions where one side is a word character (like\w) and the other side is not a word character (for instance it may be the beginning of the string or a space character).
String[] split = string.split("\\b-\\b");
Because my String is variable There always a number after --
Another solution could be by using positive lookbehind, like so (?<=\d)- :
String[] split = string.split("(?<=\\d)-");
Outputs
[dfwx--05, dfpixc3]
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