Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split this string in java

Tags:

java

string

split

Hi in my program a string is generated like "1&area_id=54&cid=3".First an integer and then a string "&area_id=",then antoher integer, and after that a string "&cid=" and then the final integer.These two strings are always same.But integer is changing.How to write split function so that i can find these three integers in three variable or with in an array.I can seperate these by looping but i want to use split function.Thanks

like image 743
Rasel Avatar asked Jan 30 '26 22:01

Rasel


1 Answers

How about

string.split("&\\w+=")

This works with your example:

System.out.println(Arrays.asList("1&area_id=54&cid=3".split("&\\w+=")));

outputs

[1, 54, 3]

The call to string.split("&\\w+=") reads in English: Split string on every match for the regular expression parameter, and then return all substrings in between the matched tokens as an array.

The regular expression reads: Match all substrings starting with "&", followed by at least ("+") one word-character ("\\w", i.e. letters, digits, and some special characters, such as the underscore from your example), followed by "=". For more details see the Javadoc for java.util.regex.Pattern

like image 160
Lukas Eder Avatar answered Feb 01 '26 15:02

Lukas Eder



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!