Java代码:
- public class StringUtil
- {
- public static void replaceBlank()
- {
- Pattern p = Pattern.compile(“//s*|/t|/r|/n”);
- String str="I am a, I am Hello ok, /n new line ffdsa!";
- System.out.println("before:"+str);
- Matcher m = p.matcher(str);
- String after = m.replaceAll("");
- System.out.println("after:"+after);
- }
- public static void main(String[] args)
- {
- replaceBlank();
- }
- }
本文介绍了一个简单的Java程序,用于演示如何使用正则表达式去除字符串中的空白字符,包括空格、制表符、换行符等。通过具体的代码示例展示了如何定义模式并使用Pattern和Matcher类来实现字符串的空白字符清除。

380

被折叠的 条评论
为什么被折叠?



