从字符串中删除所有出现的字符 [英] Remove all occurrences of char from string

查看:22
本文介绍了从字符串中删除所有出现的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以用这个:

String str = "TextX Xto modifyX";
str = str.replace('X','');//that does not work because there is no such character ''

有没有办法从 Java 中的字符串中删除所有出现的字符 X?

Is there a way to remove all occurrences of character X from a String in Java?

我试过了,这不是我想要的:str.replace('X',' ');//用空格替换

I tried this and is not what I want: str.replace('X',' '); //replace with space

推荐答案

尝试使用 采用CharSequence 参数 的重载(例如,String) 而不是 char:

Try using the overload that takes CharSequence arguments (eg, String) rather than char:

str = str.replace("X", "");

这篇关于从字符串中删除所有出现的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆