Languages

Java - split String by underscore or minus sign - how to make it work?

1 points
Asked by:
Root-ssh
178180

I have code: 

It works ok, but is it possible to have 1 split by "_" or "-" character, instead of 2 separated splits?

1 answer
2 points
Answered by:
Root-ssh
178180

We can use regex in String split method.

Regex which solves the problem: 

Full working example:

String.split method takes regex as argument:

 

0 commentsAdd comment
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join