找出字符串中连续的子字符

public class StringUtil {
public static String[] lxString(String str) {
char[] chars = str.toCharArray();
List list = new ArrayList();
for (int i = 0; i < chars.length - 1;) {
System.out.println(i);
int a = chars[i + 1] - chars[i];
StringBuffer sb = null;
if (a == 1) {
// 正序
sb = new StringBuffer();
sb.append(chars[i]);
sb.append(chars[i + 1]);
for (int j = i + 1; j < chars.length - 1; j++) {
int b = chars[j + 1] - chars[j];
if (b == 1) {
sb.append(chars[j + 1]);
i = j + 1;
} else {
i++;
break;
}
}
list.add(sb.toString());
} else {
i++;
}


}
String[] strs = new String[list.size()];
for (int i = 0; i < list.size(); i++) {
strs[i] = list.get(i);
}


return strs;
}


public static void main(String[] args) {
String[] strs = StringUtil.lxString("abcd1134456");
for (String string : strs) {
System.out.println(string);
}
}
}

你可能感兴趣的:(java)