public static Integer[] getIntegers(String str) { final Pattern p = Pattern.compile("\\d{1,4}"); Matcher matcher = p.matcher(str); ArrayList<Integer> list = new ArrayList<Integer>(); while (matcher.find()) { list.add(Integer.valueOf(matcher.group())); } return list.toArray(new Integer[list.size()]); }
public static void main(String[] args) throws Exception { String str = "udgy87539HYFnj9873465BF"; String[] arr = str.split("[^\\d+]"); for (String s : arr) { if (s.length() < 1) continue; for (int i = 0; i < s.length(); i += 4) { System.out.print (s.substring(i, i+4 > s.length() ? s.length() : i+4) + " "); } } }
public static Integer [] getIntArray (String str) { if (str == null || str.length() <= 0) return null; char [] chs = str.toCharArray(); int len = chs.length; StringBuffer str_num = new StringBuffer(); ArrayList<Integer> numbers = new ArrayList<Integer>(); for (int i = 0; i < len; i++) {
if (Character.isDigit(chs[i])) { str_num.append(chs[i]); }
if (i < len-2 && (!Character.isDigit(chs[i+1]) || str_num.length() == 4)) { if (str_num.length() > 0) { numbers.add(Integer.parseInt(str_num.toString())); System.out.println(str_num); str_num = new StringBuffer(); }
} } Integer [] s = new Integer [numbers.size()]; numbers.toArray(s); return s; }
public static Integer[] getIntegers(String str) {
final Pattern p = Pattern.compile("\\d{1,4}");
Matcher matcher = p.matcher(str);
ArrayList<Integer> list = new ArrayList<Integer>();
while (matcher.find()) {
list.add(Integer.valueOf(matcher.group()));
}
return list.toArray(new Integer[list.size()]);
}
String str = "udgy87539HYFnj9873465BF";
String[] arr = str.split("[^\\d+]");
for (String s : arr) {
if (s.length() < 1) continue;
for (int i = 0; i < s.length(); i += 4) {
System.out.print (s.substring(i, i+4 > s.length() ? s.length() : i+4) + " ");
}
}
}
public static Integer [] getIntArray (String str) {
if (str == null || str.length() <= 0)
return null;
char [] chs = str.toCharArray();
int len = chs.length;
StringBuffer str_num = new StringBuffer();
ArrayList<Integer> numbers = new ArrayList<Integer>();
for (int i = 0; i < len; i++) {
if (Character.isDigit(chs[i])) {
str_num.append(chs[i]);
}
if (i < len-2 && (!Character.isDigit(chs[i+1]) || str_num.length() == 4)) {
if (str_num.length() > 0) {
numbers.add(Integer.parseInt(str_num.toString()));
System.out.println(str_num);
str_num = new StringBuffer();
}
}
}
Integer [] s = new Integer [numbers.size()];
numbers.toArray(s);
return s;
}
public class TestEnd {
public static void main(String[] args) {
String s2="udgy87539HYFnj9873465BF";
String[] result2=s2.split("\\D");//按非数字拆分字符串
ArrayList<String> arr=new ArrayList<String>();// 存放截好的字符串
for(int i=0;i<result2.length;i++){
if(result2[i].length()>=4)//截断长度大于4
for(int j=0;j<result2[i].length();j+=4){
if(j+4>result2[i].length())//截到串尾且不够4个时
arr.add(result2[i].substring(j,result2[i].length()));
else arr.add(result2[i].substring(j,j+4));
}
else arr.add(result2[i]);
}
for(String temp:arr){//输出
System.out.println(temp);
}
}
}
学校一般不会教 Pattern 类的。public class Test4 {
public static void main(String[] args) {
String str = "udgy87539HYFnj9873465BF";
String[] strs = str.split("\\D+|(?<=\\G\\d{4})");
System.out.println(Arrays.toString(strs));
}
}