124697
124697

Reputation: 21893

How do I remove some characters from my String

i want to remove all of the following chars from my string

">[],-"

at the moment im doing this. but there must be a more efficient way

newString = myString.replace(">","").replace("[","").replace("]","")....

Upvotes: 16

Views: 51924

Answers (8)

ashwani prasad
ashwani prasad

Reputation: 1

use regex; the string relaceAll method takes the reegex as first argument and the replacement text as second argument; for example

return myString.replaceAll(">|\\[|\\]|,|-";, "");

Upvotes: 0

codaddict
codaddict

Reputation: 454920

You can use the replaceAll method of the String class.

You can form a character class consisting of the characters you want to delete. And the replacement string will be empty string "".

But the characters you want to delete which you'll be putting in the character class might be regex meta-characters and hence need to be escaped. You can manually escape them as many answers show, alternatively you can use the Pattern.quote() method.

String charToDel = ">[],-";
String pat = "[" + Pattern.quote(charToDel) + "]";
String str = "a>b[c]d,e-f";
str = str.replaceAll(pat,"");

See it

Upvotes: 9

Andreas Dolk
Andreas Dolk

Reputation: 114757

More efficient performace-wise: yes. You can't "delete" chars from a String so you have t create a new one anyway. Consider a switch/case based solution:

StringBuilder sb = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
  switch(s.getCharAt(i)) {
    case '>':
    case '[':
    case ']':
    case ',':
    case '-': break;
    default: sb.append(s.getCharAt(i));
  }
}
s = sb.toString();

Upvotes: 4

Darron
Darron

Reputation: 21618

newString = myString.replaceAll("[>\\[\\],-]", "");

The backslashes are to escape the '[' because the first argument is actually a regular expression.

Upvotes: 2

darioo
darioo

Reputation: 47183

newString = myString.replaceAll("[>\\[\\],-]", "");

Upvotes: 1

user473395
user473395

Reputation:

Using the replace / replaceAll methods causes a new Pattern to be compiled each time. So if you are doing this multiple times I would strongly suggest either looping through the characters or creating a single regular expression to use repeatedly.

Upvotes: 2

Karl Knechtel
Karl Knechtel

Reputation: 61479

Use a regex that describes all the characters you want to replace, with the method that replaces everything matching the regex:

newString = myString.replaceAll("[<>\\[\\],-]", "");

(edited: I don't think <> are supposed to be escaped, actually. And I forgot to double up the backslashes since they'll be interpreted twice: once by the Java compiler, and again by the regular expression engine.)

Upvotes: 20

Jonathan Wood
Jonathan Wood

Reputation: 67175

You could loop through each character, appending them one by one to a StringBuilder() unless they match one of the characters you are removing.

But I'm not sure that'd be a whole lot faster.

Upvotes: 0

Related Questions