比如 String a = "5分钟";这个5分钟有可能是10分钟,或者20分钟这种不确定的数。
如何只截取数字部分呢

解决方案 »

  1.   


    String str = "35分钟";
    int index = str.indexOf("分钟");
    String result = "0";
    if( index > 0 )
    {
         result = str.subString(0,index);
    }System.out.println(result);
      

  2.   

    String[] strArray = str.split("分钟");
    or 
    String strOutput = str.replaceAll("分钟", "");
      

  3.   

    "分钟"2个字的长度是固定的,所以....a.substring(0, a.length()-2)
      

  4.   

    这样 public static void main(String[] args) {
    String str = "拉拉5分钟呵呵10分钟";
    String regex  = "[\\d]+分钟"; 
    Pattern pattern = Pattern.compile(regex);
    Matcher m = pattern.matcher(str);
    while(m.find()) {
    String matched = m.group();
    System.out.println(matched.substring(0,matched.indexOf("分钟")));
    }
    }
      

  5.   

    用正则表达式吧:
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;public class RegTest
    {
    public static void main(String[] args){
    Pattern p = Pattern.compile("[0-9]+");
    Matcher m = p.matcher("10分钟");
    if(m.find()){
    System.out.println(m.group());
    }
    }
    }
      

  6.   


    public static void main(String args[]) {
            String str = "*分钟";
    String target= str.replaceAll("分钟", "");
    //System.out.println(target);
    }
      

  7.   

    public class test
    {
    public static void main(String args[])
    {
    String str="523242分zhonga";
    byte b[] = str.getBytes();
    for(int i=0;i<b.length;i++)
    {
    System.out.println(""+i);
    System.out.println(b[i]);
    if(Character.isDigit(b[i]))
    continue;
    else b[i]=' ';

    }
    String str2= new String(b);
    str2=str2.trim();
    System.out.println(str2);
    }
    }
      

  8.   

    http://bbs.tarena.com.cn/去这里看看