隐藏

JAVA 去除字符串前后的指定字符

发布:2022/4/26 11:20:00作者:管理员 来源:本站 浏览次数:1050

为了显示效果更好,我们可以将多余的字符去掉,代码如下:

		
  1. /**
  2. *去掉字符串前后的指定字符
  3. */
  4. public static String trimBothChars(String str, String splitter) {
  5. String regex = "^" + splitter + "*|" + splitter + "*$";
  6. return str.replaceAll(regex, "");
  7. }

效果:

		
  1. oldStr:,this is txt,
  2. newStr:this is txt