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


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

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));
    }

}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM