java8 查找字符串里面第二次出现的冒号后面的内容
可以使用indexOf()方法和substring()方法来实现。
首先,使用indexOf()方法找到第一次出现冒号的位置,并将其存储在变量firstIndex中。然后,使用indexOf()方法从firstIndex + 1的位置开始找到第二次出现冒号的位置,并将其存储在变量secondIndex中。
最后,使用substring()方法从secondIndex + 1的位置开始截取字符串,即可获取冒号后面的内容。
以下是示例代码:
public class Main {
public static void main(String[] args) {
String str = "abc:def:ghi:jkl";
int firstIndex = str.indexOf(":");
int secondIndex = str.indexOf(":", firstIndex + 1);
String result = str.substring(secondIndex + 1);
System.out.println(result);
}
}
输出结果为:"jkl"
原文地址: https://www.cveoy.top/t/topic/hWcw 著作权归作者所有。请勿转载和采集!