求一方法 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" 中的一种,仅且只要一种 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 用String的split函数先用分号split,然后用等号split 只要数字!比如说:参数:str="P"或者str="N"或者str="M";则函数返回:803如果:str="T"或者str="E"则返回808; 最快的方法 str="P" || "N" || "M" || "T" || "E" testInfo= "803||808";凑人气的 帮你顶 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"; } }} public static int test(Character ch){ return "PNM".contains(ch.toString()) ? 803 : ("TE".contains(ch.toString()) ? 807 : -1);} 拿str用toCharArray()方法转换成字符数组然后判断这个字符数组的每个元素是不是在ASCII码的48-57之间如果是的话,直接拼接就好了这样就能取出所有的数字了 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⊙) 用正则表达式吧,下边返回的是 803808 需要什么再截取吧public static int test(String str){ String numStr = str.replaceAll("[^0-9]", ""); int num = Integer.valueOf(numStr); return num; } 重写个public static String[] test(String str){ String str1 = str.replaceAll("[^0-9=]", ""); String[] strs = str1.split("="); return strs; } 如果要得到的是 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]); } }}测试结果803808 java的内存问题 solrJ 分组查询返回结果老是报错 jdbc插入20w条数据到数据库 性能优化 请教一个关于String的问题 将数组内容写入文件并读出 关于Java和MySql的连接问题 dom输出字符串问题??????? 急!!!!!关于画点算法的实现 为什么我听不到声音,请高手帮忙!!!! 关于java里类数组 怎么实现人物与地图的碰撞检测?? 类内方法外new 对象语句是如何被执行的?
先用分号split,然后用等号split
只要数字!
比如说:参数:str="P"或者str="N"或者str="M";则函数返回:803
如果:str="T"或者str="E"则返回808;
testInfo= "803||808";
凑人气的 帮你顶
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";
}
}
}
return "PNM".contains(ch.toString()) ? 803 : ("TE".contains(ch.toString()) ? 807 : -1);
}
然后判断这个字符数组的每个元素是不是在ASCII码的48-57之间
如果是的话,直接拼接就好了
这样就能取出所有的数字了
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⊙)
String numStr = str.replaceAll("[^0-9]", "");
int num = Integer.valueOf(numStr);
return num;
}
String str1 = str.replaceAll("[^0-9=]", "");
String[] strs = str1.split("=");
return strs;
}
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