简体   繁体   中英

How to split this string in java

嗨,在我的程序中生成一个字符串,如"1&area_id=54&cid=3" 。首先是整数,然后是字符串"&area_id=" ,然后是antoher整数,然后是字符串“&cid =”,然后是最后的整数。这两个字符串总是相同的。但整数正在改变。如何编写分割函数,以便我可以在三个变量中或在数组中找到这三个整数。我可以通过循环分离这些但我想使用分割函数。谢谢

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" , ie 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM