1. 原生java方式 先判断是否为空
public static String removeLastChar(String s) { |
2. 使用java8
public static String removeLastCharOptional(String s) { |
3. 使用 apache common lang StringUtils.substring()
String TEST_STRING = "ACBDEF"; |
4. 使用 StringUtils.chop()方法 因对边缘场景的情况(empty or null)时
StringUtils.chop(TEST_STRING) |
5. 使用replaceAll() 的正则表达式方式
public static String removeLastCharRegex(String s) { |