0
点赞
收藏
分享

微信扫一扫

String应该这么玩

当需要替换一个字符串中的某个字符,你会怎么做?

方法一:使用replace()

String str = "Hello, world!";
String newStr = str.replace(",", ""); // 去掉逗号
System.out.println(newStr); // 输出Hello world!

方法二:使用repalceAll()正则的方式

String str = "   Hello     world!    ";
String newStr = str.replaceAll("\\s+", "");
System.out.println(newStr); // 输出Helloworld!

其中\\s+代表删除一个或者多个空格

正则表达式学习的路径:https://blog.csdn.net/daima_trash/article/details/122276518

举报

相关推荐

0 条评论