java 使用正则去除字符串首尾两端指定的字符

it2022-05-09  26

java 使用正则去除字符串首尾两端指定的字符 且中间的不做处理

/** * 去除字符串首尾两端指定的字符 * */ public static String trimBothEndsChars(String srcStr, String splitter) { String regex = "^" + splitter + "*|" + splitter + "*$"; return srcStr.replaceAll(regex, ""); } public static void main(String[] args) { String str = "***内容1*内容2**"; String o = trimBothEndsChars(str,"\\*"); System.out.println("结果:" + o); }

运行结果: 结果:内容1*内容2


最新回复(0)