justDrink
justDrink

Reputation: 43

how to split a number without losing other same characters in Java


I was using Eclipse for Java and I want to split a number without losing other same characters.

For example, the input line is:
[1142,143,2142,142]

the output should be like that:
1142 143 2142


i was using split("142|\\D+")but the output was showing like this:
1 143 2

What should I do ?

Upvotes: 0

Views: 41

Answers (3)

Rajesh
Rajesh

Reputation: 2155

Replace brackets and split:

String value = "[1142,143,2142,142]";

String xl = value.replaceAll("[\\[\\]]", "");

String splitted[] = xl.split(",");

for (String string : splitted)          
    if (!string.matches("142"))
        System.out.println(string);

Upvotes: 1

Avinash Raj
Avinash Raj

Reputation: 174696

You need to use word boundaries.

string.split("\\b142\\b|\\D+");

OR

Do replace and then split.

string.replaceAll("\\b142\\b|[\\[\\]]", "").split(",");

Upvotes: 1

vks
vks

Reputation: 67968

,142|\\D+

You can split by this.See demo.

https://regex101.com/r/pG1kU1/30

Upvotes: 0

Related Questions