user1545072
user1545072

Reputation:

String.split() by one of two patterns

I want to split this string: 315-045/10-20 to this array: ["315","045","10","20"], meaning it should be split around every occurrence of '/' or '-'. Is it possible to do it with one call to the split() function?

Upvotes: 2

Views: 1190

Answers (3)

Reimeus
Reimeus

Reputation: 159844

You could alternatively use a non-digit split pattern:

"315-045/10-20".split("\\D");

Upvotes: 4

xagyg
xagyg

Reputation: 9721

You can use this ...

split("[-/]")

Upvotes: 2

FThompson
FThompson

Reputation: 28697

You can use a regex which accepts both slashes and dashes.

String input = "315-045/10-20";
String[] output = input.split("[/-]");

Upvotes: 5

Related Questions