Java 从字符串中删除特定字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20769908/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 04:12:53  来源:igfitidea点击:

Remove specific char from String

javaandroidstringremoveall

提问by M.ArslanKhan

How To remove a specific Character from a String. I have a Arraylist testingarray.

如何从字符串中删除特定字符。我有一个 Arraylist testingarray。

String line=testingarray.get(index).toString();

I want to remove a specific character from line.

我想从行中删除特定字符。

I have Array of uniCodes

我有 uniCode 数组

int uniCode[]={1611,1614,1615,1616,1617,1618};

i want to remove those characters that have these Unicodes.

我想删除那些具有这些 Unicode 的字符。

采纳答案by Shayan Pourvatan

use :

用 :

NewString = OldString.replaceAll("char", "");

in your Example in comment use:

在您的评论示例中使用:

NewString = OldString.replaceAll("d", "");

for removing Arabic character please see following link

要删除阿拉伯字符,请参阅以下链接

how could i remove arabic punctuation form a String in java

我怎么能在java中从字符串中删除阿拉伯标点符号

removing characters of a specific unicode range from a string

从字符串中删除特定 unicode 范围的字符

回答by No_Rulz

Try this,

尝试这个,

String result = yourString.replaceAll("your_character","");

Example:

例子:

String line=testingarray.get(index).toString();
String result = line.replaceAll("[-+.^:,]","");

回答by Su-Au Hwang

If it's a single char, there is no need to use replaceAll, which uses a regular expression. Assuming "H is the character you want to replace":

如果是单个字符,则无需使用replaceAll,它使用正则表达式。假设“H 是您要替换的字符”:

String line=testingarray.get(index).toString();
String cleanLine = line.replace("H", "");

update (after edit): since you already have an int array of unicodes you want to remove (i'm assuming the Integers are decimal value of the unicodes):

更新(编辑后):因为您已经有一个要删除的 unicode 整数数组(我假设整数是 unicode 的十进制值):

String line=testingarray.get(index).toString();
int uniCodes[] = {1611,1614,1615,1616,1617,1618};
StringBuilder regexPattern = new StringBuilder("[");
for (int uniCode : uniCodes) {
    regexPattern.append((char) uniCode);
}
regexPattern.append("]");
String result = line.replaceAll(regexPattern.toString(), "");

回答by Visruth

you can replace character using replace method in string.

您可以使用字符串中的替换方法替换字符。

String line = "foo";
line = line.replace("f", "");
System.out.println(line);

output

输出

oo