3

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.

M.ArslanKhan
  • 2,604
  • 5
  • 29
  • 43

4 Answers4

14

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

Community
  • 1
  • 1
Shayan Pourvatan
  • 11,622
  • 4
  • 39
  • 62
  • thanks . i did it but i did't get what i want. Basically i want i have 7 unicodes of Arabic characters , int uniCode[]={1611,1614,1615,1616,1617,1618}; i want to remove those words that have these unicodes – M.ArslanKhan Dec 25 '13 at 07:12
  • @ Shayan pourvatan thanks a lot . That is what i was searching i got my solution. – M.ArslanKhan Dec 25 '13 at 15:34
7

you can replace character using replace method in string.

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

output

oo
Visruth
  • 3,138
  • 30
  • 45
2

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(), "");
Su-Au Hwang
  • 4,131
  • 16
  • 23
1

Try this,

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

Example:

String line=testingarray.get(index).toString();
String result = line.replaceAll("[-+.^:,]","");
No_Rulz
  • 2,626
  • 1
  • 15
  • 32
  • replaceAll("[-+.^:,]","") - this is working as a regEx and if anything is present in line , it is replacing with blank( trimming it). – satender Jan 09 '18 at 15:02