写一个方法method1(String s),返回字符串参数中最后一个数字。例:“His telephone number is 123456789” return 123456789;
"There are 1 bees,2 butterflies,3 dragonflies" return 3;
"There are 1 bees,2 butterflies,3 dragonflies" return 3;
2:s1.substring(s1.length()-1,s1.length())
public class TestInterceptString {
public static void main(String[] args) {
String str = "There are 1 bees,2 butterflies,3 dragonflies";
str = method(str);
System.out.println(str); } public static String method(String str) {
String[] temps = str.split("[^0-9]");
str = temps[temps.length - 1];
return str;
}
}
public static void main(String[] args){
String s1="His telephone number is 123456789";
String s2="There are 1 bees,2 butterflies,3 dragonflies";
String s3 = "";
StringIntercept si = new StringIntercept();
s3 = si.method(s1);
System.out.println(s3);
s3 = si.method(s2);
System.out.println(s3);
}
public static String method(String str){
String[] s;
String length = "";
s = str.split(",");
length = s[s.length - 1]; if(str == length){
str = s[s.length - 1];
s = str.split(" ");
}
str = s[s.length - 1];
s = str.split("[^0-9]");
str = s[s.length - 1];
return str;
}
}