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