Remove specific char from String [closed]

左心房为你撑大大i 提交于 2019-12-03 15:17:36

问题


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

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

I want to remove a specific character from line.

I have Array of uniCodes

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

i want to remove those characters that have these Unicodes.


回答1:


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

removing characters of a specific unicode range from a string




回答2:


you can replace character using replace method in string.

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

output

oo



回答3:


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":

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):

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(), "");



回答4:


Try this,

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

Example:

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


来源:https://stackoverflow.com/questions/20769908/remove-specific-char-from-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!