替换字符串中的字符 [英] replace a char in String

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

问题描述

我想替换字符串中的字符.我的问题是,一开始你不知道它是哪个字符,所以在某些情况下,当我的字符是+"时,我会收到一条错误消息.我不希望我的字符被解释为正则表达式,那我该怎么办?

Hi I'd like to replace a char in a String. My problem is that at first you don't know which char it is, so in some cases I get an error message when my char is for example '+'. I don't want my char being interpreted as regex, so what should I do?

可能代码应该是这样的:

May code should be something like this:

String test = "something";
char ca = input.chatAt(0);
input = input.replaceAll("" + ca, "");

希望你能帮我.

推荐答案

那就不要使用正则表达式.

Just don't use regex then.

input = input.replace(String.valueOf(ca), "");

StringreplaceAll 方法将正则表达式的 String 表示作为参数.

The replaceAll method of String takes the String representation of a regular expression as an argument.

replace 方法没有.

请参阅API.

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

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