java中怎么用string移除字符串

621
2023/11/16 9:35:23
栏目: 编程语言
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

在Java中,可以使用replaceAll()方法来移除字符串。该方法可以使用正则表达式来指定要移除的字符串。

下面是一个示例代码:

public class RemoveString {
    public static void main(String[] args) {
        String str = "Hello, World!";
        String removedStr = str.replaceAll("World", "");
        System.out.println(removedStr);  // 输出:Hello, !
    }
}

在上面的示例中,我们使用replaceAll()方法将字符串中的"World"替换为空字符串,从而实现了移除该字符串的效果。

辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读: Java实现多线程同步五种方法详解