日期:2014-05-20  浏览次数:20795 次

截取字符串
Java code
String t = "馆藏复本:5 可借复本:5";

我想截取上面字符串中的两个数字,不要用subString,有其他方法吗?比如正则表达式。因为中间的数字不一定是一位数

------解决方案--------------------
Pattern p = Pattern.complie("[^\\d]+(\\d+)[^\\d]+(\\d+)");
String t = "馆藏复本:5 可借复本:5";
Matcher m = p.matcher(t);
if(m.find()){
String 馆藏复本 = m.group(1);
String 可借复本 = m.group(2);
}
------解决方案--------------------
String t = "馆藏复本:5 可借复本:5";
t = t.replaceAll("[^x00-xff]"," ");
System.out.println(Arrays.toString(t.trim().split("\\s+")));
------解决方案--------------------
String t = "馆藏复本:5 可借复本:5";
String[] data = t.replaceAll("[^\\d]+(\\d+)[^\\d]+(\\d+)","$1,$2").split(",");
------解决方案--------------------
String t = "馆藏复本:5 可借复本:5";
String[] data = t.replaceAll("[^\\d]+(:)"," ").trim().split(" ");
System.out.println(Arrays.toString(data));
------解决方案--------------------
Java code

String t = "馆藏复本:5 可借复本:5";
String[] str = t.split("[^0-9]+");
System.out.println("馆藏复本:" + str[1] + "可借复本: " + str[2]);