问题
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