简体   繁体   English

如何在java中拆分此字符串

[英]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. string.split("&\\\\w+=")的调用以英语读取:在正则表达式参数的每个匹配上拆分string ,然后将匹配的标记之间的所有子字符串作为数组返回。

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 "=" . 正则表达式为:匹配所有以"&"开头的子串,后跟至少( "+" )一个字符( "\\\\w" ,即字母,数字和一些特殊字符,例如您的下划线)例子),然后是"=" For more details see the Javadoc for java.util.regex.Pattern 有关更多详细信息,请参阅java.util.regex.Pattern的Javadoc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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