有固定的前后节点规定么??
用substring()也许可以。循环也可以。

解决方案 »

  1.   

    0-9的字符对应于一定的数值(具体的忘记了,你可以在程序中赋两个char类型为‘0’和‘9’,然后跟踪看看它对应的数值),根据数值区间判断它们是否是数值,然后用Integer.parseInt转换就可以了。
      

  2.   

    我帮你写的函数只能应付字符串中只有一个你想要的子字符串,如果有多个的话,只能取出第一个,别的就不取了,你自己可以看我的代码修改适用多个的情况:
      public String bbc(String strValue, String chrValue) {
        String str_Value = strValue;
        String car_Value = chrValue;
        String strTmp="";
        String str_return = "none";
        int isave,icount, i;
        isave = 0;
        try {
          icount = str_Value.length();
          while (isave < icount) {
            if (strValue.indexOf(chrValue, isave) != -1) {
              if (strValue.substring(isave, isave+2)=="12"){
                str_return=strValue.substring(isave, isave+2);
                return str_return;
              }
            }
          }
        }
        catch (Exception e) {    }
        return str_return;
      }
      

  3.   

    有些错误:改正为
      public String bbc(String strValue, String chrValue) {
        String str_Value = strValue;
        String chr_Value = chrValue;
        String strTmp="";
        String str_return = "none";
        int isave,icount;
        isave = 0;
        try {
          icount = str_Value.length();
          while (isave < icount) {
            if (str_Value.indexOf(chr_Value, isave) != -1) {
              if (str_Value.substring(isave, isave+2)=="12"){
                str_return=str_Value.substring(isave, isave+2);
                return str_return;
              }
            }
          }
        }
        catch (Exception e) {    }
        return str_return;
      }
      

  4.   

    楼主做这个具体要干什么,是不是要做一些课件:具体地说你可以用写一个需要三个参数的方法,第一个参数是包含数字的那个大字符串,第二个参数传你要取得那个数字的前面的几个字符,如12前面是“需要”,第三个参数是后面的字符,如12后面是“分钟”,然后可以用indexOf()取到第一个子串“需要”出现的位置,加子串长度,然后取第二个字串“分钟”的位置,用substring()根据两个参数,从第一个参数中取出数字,两个字串必须足够场,可以唯一标示出数字的位置,这样你的数字变也没关系,相同的题目可以有不同的数字,得不同的结果。
      

  5.   

    String s="从第1站走到第2站需要12分钟";
    String result=s.substring(s.indexOf("需要")+4,s.indexOf("分钟"));
    如果你要得到int型的
    int i=Integer.parseInt(result);
      

  6.   

    非常感谢大家帮忙,但由于提问题的当时我很忙,忘了上来看大家的高招,耽误大家的时间了.
    由于我要处理的字符串中有没有数字是不确定的,我编了一个方法,请批评指正.
     public int getNumber(String str){
          int num = -1;
          char  charTemp;
          String strTemp = "";
          char charNum[] = new char[20];
          int ascValue = 0;
          int p = 0;      for(int i=str.length()-1;i>=0;i--){
              charTemp = str.charAt(i);
              ascValue = (int)charTemp;
              if(((ascValue>=48)&&(ascValue<=57))||((ascValue>=65296)&&(ascValue<=65305))){
                  charNum[i]=charTemp;
                  p=i;
              }
              else{
                  if(p==i+1){
                      break;
                  }
              }
          }
          for(int j=0;j<charNum.length;j++){//int j=charNum.length-1;j>=0;j--){
              if(charNum[j]!='\u0000'){
                  strTemp = strTemp.concat(new Character(charNum[j]).toString());
              }
          }
          if(strTemp.length()>0){
              num = Integer.parseInt(strTemp);
          }
          return num;
       }
      

  7.   

    判断一下数字的后是否为数字.如true,则取出这段数字(即取出直到不是数字的前一位为止)