日期:2014-05-20 浏览次数:20835 次
public static void main(String[] args) throws IOException { String str="284_300_16_331_34_363_379_395"; String temp; if("_".equals(str.substring(str.length()-1,str.length()))){ temp=str.substring(0,str.lastIndexOf("_")); }else{ temp=str; } System.out.println(temp.substring(temp.lastIndexOf("_")+1,temp.length())); }
------解决方案--------------------
String s ="284_300_16_331_34_363_379_395_"; String s1=s; if(s.lastIndexOf("_")==s.length()-1){ s1 =s.substring(0,s.length()-1); } String last =s1.substring(s1.lastIndexOf("_")+1); System.out.println(last);
------解决方案--------------------
String _s = "284_300_16_331_34_363_379_395_"; String _s0 = _s.substring(0, _s.length-1); int _last_index = _s0.lastIndexOf("_"); String _result = _s0.substring(_last_index+1);
------解决方案--------------------
Pattern pattern = Pattern.compile("[0-9]{2,3}$");
Matcher matcher = pattern.matcher("234_1231_234_34_34_345_45_34");
if(matcher.find())
System.out.println(matcher.group());
------解决方案--------------------
String str="284_300_16_331_34_363_379_395_";
String[] s=str.split("_");
System.out.println(s[s.length-1]);
------解决方案--------------------
//这是楼上各位的,都很强大 import java.util.regex.Matcher; import java.util.regex.Pattern; public class Split5 { public static void main(String args[]) { String s2 = "284_300_16_331_34_363_379_395_"; String[] ss = s2.split("_"); System.out.println(ss[ss.length - 1]); // String str = "284_300_16_331_34_363_379_395"; String temp; if ("_".equals(str.substring(str.length() - 1, str.length()))) { temp = str.substring(0, str.lastIndexOf("_")); } else { temp = str; } System.out.println(temp.substring(temp.lastIndexOf("_") + 1, temp .length())); // String s ="284_300_16_331_34_363_379_395_"; String s1=s; if(s.lastIndexOf("_")==s.length()-1){ s1 =s.substring(0,s.length()-1); } String last =s1.substring(s1.lastIndexOf("_")+1); System.out.println(last); // String sq = "284_300_16_331_34_363_379_395_"; String _s0 = sq.substring(0, sq.length()-1); int _last_index = _s0.lastIndexOf("_"); String _result = _s0.substring(_last_index+1); System.out.println(_result); // Pattern pattern = Pattern.compile("[0-9]{2,3}$"); Matcher matcher = pattern.matcher("234_1231_234_34_34_345_45_34"); if(matcher.find()) System.out.println(matcher.group()); } }