Java 字符串截取:获取两个'-'之间的内容
你可以使用 substring 方法来实现这个功能。下面是一个示例代码:
public class Main {
    public static void main(String[] args) {
        String str = "Java-字符串-截取";
        int firstIndex = str.indexOf("-");
        int secondIndex = str.indexOf("-", firstIndex + 1);
        String result = str.substring(firstIndex + 1, secondIndex);
        System.out.println(result); // 输出 "字符串"
    }
}
在这个例子中,我们使用 indexOf 方法来查找第一个和第二个 "-" 的位置,并使用 substring 方法来截取这两个位置之间的字符串。最后,将截取到的字符串打印出来。
原文地址: https://www.cveoy.top/t/topic/p4TK 著作权归作者所有。请勿转载和采集!