java8 查找字符串里面第三次出现的冒号的内容 不适用正则
可以使用循环遍历字符串的方式来查找第三次出现的冒号的内容。代码如下:
public class Main {
public static void main(String[] args) {
String str = "abc:def:ghi:jkl:mno:pqr:stu";
int count = 0; // 计数器
int index = -1; // 冒号的索引位置
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == ':') {
count++; // 冒号计数器加1
if (count == 3) {
index = i; // 获取第三个冒号的索引位置
break; // 找到后跳出循环
}
}
}
if (index != -1) {
// 获取冒号后的内容
String content = str.substring(index + 1);
System.out.println(content);
} else {
System.out.println("字符串中没有第三个冒号");
}
}
}
输出结果为:"mno:pqr:stu"
原文地址: https://www.cveoy.top/t/topic/hWcX 著作权归作者所有。请勿转载和采集!