java8 查找字符串里面第二次出现的冒号的内容 不适用正则
你可以使用indexOf()和substring()方法来实现找到字符串中第二次出现冒号的内容。以下是一个示例代码:
public class Main {
public static void main(String[] args) {
String str = "abc:def:ghi:jkl:mno:pqr";
int firstIndex = str.indexOf(":");
int secondIndex = str.indexOf(":", firstIndex + 1);
if (secondIndex != -1) {
String content = str.substring(firstIndex + 1, secondIndex);
System.out.println("第二次出现冒号的内容为:" + content);
} else {
System.out.println("字符串中没有第二次出现冒号的内容");
}
}
}
输出结果为:
第二次出现冒号的内容为:def
首先使用indexOf(":")方法找到第一个冒号的索引,然后使用indexOf(":", firstIndex + 1)方法找到第二个冒号的索引。如果第二个冒号存在,则使用substring()方法获取从第一个冒号后一位到第二个冒号前一位的内容。如果第二个冒号不存在,则输出相应的提示信息
原文地址: https://www.cveoy.top/t/topic/hWcL 著作权归作者所有。请勿转载和采集!