[英]How to split string if different character occur?
I have a string like below: 我有一个如下字符串:
String str = "77755529";
I want to split this string if different number occur ie result should be like below after splitting : 如果出现不同的数字,我想分割这个字符串,即分割后结果应如下所示:
str1 = "777";
str2 = "555";
str3 = "2";
str4 = "9";
I tried it with split but could not make it. 我尝试分裂但无法做到。
Try this: 尝试这个:
String str = "77755529";
String[] res = str.split("(?<=(.))(?!\\1)");
You may do matching. 你可以做匹配。
List<String> lst = new ArrayList<String>();
Matcher m = Pattern.compile("(\\d)\\1+|\\d+").matcher(s);
while(m.find()) {
lst.add(m.group());
}
System.out.println(lst);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.