可以使用 String 类的 substring() 方法和 lastIndexOf() 方法来实现取最后一个逗号之前的字符串。

示例代码:

String str = "hello,world,java,";
int lastIndex = str.lastIndexOf(",");
if (lastIndex != -1) { // 如果字符串中存在逗号
    String result = str.substring(0, lastIndex); // 取最后一个逗号之前的子串
    System.out.println(result);
}

输出结果:

hello,world,java
java取最后一个逗号之前的字符串

原文地址: https://www.cveoy.top/t/topic/fTxO 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录