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
  • 3,640
  • 8
  • 34
  • 56

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,898
  • 4
  • 42
  • 63
  • 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,430
  • 35
  • 48
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(), "");
Suau
  • 4,628
  • 22
  • 28
1

Try this,

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

Example:

String line=testingarray.get(index).toString();
String result = line.replaceAll("[-+.^:,]","");
No_Rulz
  • 2,679
  • 1
  • 20
  • 33
  • 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