开发者

How to split this string in java

开发者 https://www.devze.com 2023-03-05 03:04 出处:网络
Hi in my program a string is g开发者_StackOverflowenerated like \"1&area_id=54&cid=3\".First an integer and then a string \"&area_id=\",then antoher integer, and after that a string \"&

Hi in my program a string is g开发者_StackOverflowenerated 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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消