当前位置: 代码迷 >> J2EE >> java字符串 截取,该怎么解决
  详细解决方案

java字符串 截取,该怎么解决

热度:751   发布时间:2016-04-22 02:43:18.0
java字符串 截取
各位 
我想获取
284_300_16_331_34_363_379_395_ 字符串中最后一个数(395(也有可能是两位数))
用java怎么获取
字符串长度不定  

谢了!!

------解决方案--------------------
Java code
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()));}
------解决方案--------------------
探讨

Java code

String s = "284_300_16_331_34_363_379_395_";
String[] ss= s.split("_");
System.out.println(ss[ss.length - 1]);

------解决方案--------------------
String s = "284_300_16_331_34_363_379_39_";
String[] ss= s.split("_");
System.out.println(ss[ss.length - 1]);
------解决方案--------------------
呃。。。lz说这个字符串长度不固定,也就是说如果很长的话用“_”分割会产生一个长度很长的数组。这样会不会太占用资源。如果这样实现的话 大家看有没有什么问题:
Pattern pattern = Pattern.compile("[0-9]*$");
Matcher matcher = pattern.matcher("234_1231_234_34_34_345_45_34");\
if(matcher.find())
System.out.println(matcher.group());
------解决方案--------------------
探讨

Java code

String s = "284_300_16_331_34_363_379_395_";
String[] ss= s.split("_");
System.out.println(ss[ss.length - 1]);

------解决方案--------------------
Java code
        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);
------解决方案--------------------
Java code
  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]);
------解决方案--------------------
Java code
//这是楼上各位的,都很强大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());    }}
  相关解决方案