public static String removeTrailingX(String str) { int i = str.length() - 1; while (i >= 0 && (str.charAt(i) == 'x' || str.charAt(i) == 'X')) { i--; } return str.substring(0, i+1); }

// 示例 String s1 = 'hello worldXxxX'; String s2 = 'xxxxxx'; String s3 = 'NoX'; System.out.println(removeTrailingX(s1)); // 输出 'hello world' System.out.println(removeTrailingX(s2)); // 输出 '' System.out.println(removeTrailingX(s3)); // 输出 'No'

Java 字符串格式化函数:移除末尾的 'x' 或 'X'

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

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