java截取一个字符串正数或倒数某个特定字符前后的内容

时间:2018-01-24 23:36:25   收藏:0   阅读:927

取出正数第二个“.”后面的内容

public class TestCode {
    public static void main(String[] args) {
        String str ="232ljsfsf.sdfl23.ljsdfsdfsdfss.23423.sdfsdfsfd";
        //获得第一个点的位置
        int index=str.indexOf(".");
        System.out.println(index);
        //根据第一个点的位置 获得第二个点的位置
        index=str.indexOf(".", index+1);
        //根据第二个点的位置,截取 字符串。得到结果 result
        String result=str.substring(index);
        //输出结果
        System.out.println(result);
    }

}

取出倒数第三个“-”前面的内容

public class subString {
    public static void main(String[] args) {
        String b = "/dota-2/talent/arc-warden-20-2-38";
        String subStringB = b.substring(b.lastIndexOf("/")+1);
        int index=subStringB.lastIndexOf("-");
        index=subStringB.lastIndexOf("-", index-1);
        index=subStringB.lastIndexOf("-",index-1);
        System.out.println(subStringB.substring(0,index));
    }

}

 

原文:https://www.cnblogs.com/Java-Starter/p/8343769.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!