public static int test(String str){
String testInfo = "P&N&M=803;T&E=808";
int num = 0;
// Method
……
return num;
} 根据传入的参数str 如何简单、快速、方便地取得  803 或者 808 呢其中传入的字符串String 为仅为"P" || "N" || "M" || "T" || "E" 中的一种,仅且只要一种

解决方案 »

  1.   

    用String的split函数
    先用分号split,然后用等号split
      

  2.   


    只要数字!
    比如说:参数:str="P"或者str="N"或者str="M";则函数返回:803
    如果:str="T"或者str="E"则返回808;
      

  3.   

    最快的方法 str="P" || "N" || "M" || "T" || "E"
     
              testInfo= "803||808";
    凑人气的 帮你顶
      

  4.   


    public class Test {
        public static void main(String[] args) {
           System.out.println(checkStr("P"));
           System.out.println(checkStr("N"));
           System.out.println(checkStr("M"));
           System.out.println(checkStr("T"));
           System.out.println(checkStr("E"));
           System.out.println(checkStr("S"));
        }
        public static String checkStr(String str){
            String regexOne="[PNM]";
            String regexTwo="[TE]";
            if(str.matches(regexOne)){
                return "803";
            }else if(str.matches(regexTwo)){
                return "808";
            }else{
                return "param is error";
            }
        }
    }
      

  5.   

    public static int test(Character ch){
        return "PNM".contains(ch.toString()) ? 803 : ("TE".contains(ch.toString()) ? 807 : -1);
    }
      

  6.   

    拿str用toCharArray()方法转换成字符数组
    然后判断这个字符数组的每个元素是不是在ASCII码的48-57之间
    如果是的话,直接拼接就好了
    这样就能取出所有的数字了
      

  7.   


    public static int test(String str){
    String testInfo = "P&N&M=803;T&E=808";
    String[] s = testInfo.split(";");
    String s1 = s[0];
    String s2 = s[1];
    if(s1.contains(str)){
    System.out.println(s1.substring(s1.indexOf("=")+1));
    } else if(s2.contains(str)){
    System.out.println(s2.substring(s2.indexOf("=")+1));
    }
    return 0;
    }这样的话,不知可行否 (⊙o⊙)  
      

  8.   

    用正则表达式吧,下边返回的是 803808 需要什么再截取吧public static int test(String str){
             String numStr = str.replaceAll("[^0-9]", "");
             int num = Integer.valueOf(numStr);
             return num;
         }
      

  9.   

    重写个public static String[] test(String str){
            String str1 = str.replaceAll("[^0-9=]", "");
            String[] strs = str1.split("=");
            return strs;
        }
      

  10.   

    如果要得到的是 int 话就用下边这个
    public class Test { public static int[] test(String str){
            String numStr = str.replaceAll("[^0-9;]", "");
            String[] strs = numStr.split(";");
            int[] nums = new int[strs.length];
            for (int i = 0; i < strs.length; i++) {
    nums[i] = Integer.valueOf(strs[i]);
    }
            return nums;
        } public static void main(String[] args) {
    String testInfo = "P&N&M=803;T&E=808";
    int[] nums = test(testInfo);
    for(int i = 0; i < nums.length; i++){
    System.out.println(nums[i]);
    }
    }
    }
    测试结果
    803
    808