java如果字符串最后一个字符是逗号则去除
可以使用Java中的String类的substring()方法和endsWith()方法来实现去除字符串最后一个字符的逗号。
代码示例:
public class Main {
public static void main(String[] args) {
String str = "Hello, World,";
if (str.endsWith(",")) {
str = str.substring(0, str.length() - 1);
}
System.out.println(str);
}
}
输出结果:
Hello, World
在上述代码中,我们首先判断字符串的最后一个字符是否是逗号,如果是,则使用substring()方法去除最后一个字符。最后,输出去除逗号后的字符串。
原文地址: https://www.cveoy.top/t/topic/h1Dh 著作权归作者所有。请勿转载和采集!