17

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("]","")....
2
  • Well, you could use regex, but then you'd have to escape special chars. And what kind of efficiency are you talking about? Runtime or your own (i.e. lines of code)? Commented Dec 13, 2010 at 15:06
  • Worrying about the “efficiency” of this particular problems indicates premature optimization; I highly doubt that this method is the bottleneck of your application. Please take your time to find out where your application is really slow and in need of optimization. Commented Dec 13, 2010 at 15:20

8 Answers 8

21

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

Sign up to request clarification or add additional context in comments.

1 Comment

What if I want to keep new lines ?
9

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

Comments

4

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

1 Comment

There's a typo in the code: it's not s.getCharAt(i) but s.charAt(i)
3
newString = myString.replaceAll("[>\\[\\],-]", "");

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

3 Comments

Uhh... you are going to want to wrap those characters in a character class ([]) because you are looking for any of them on their own, not a sequence of all of them in a row...
Don't think that is going to work. That will only match cases that the symbols are all together in that order.
Its still not working. I'm getting all of those characters in my string
2

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.

1 Comment

Please don't loop through the characters yourself; but do "create the regex" (a new Pattern instance, in Java) if this is important. See the Java doc for java.util.regex.Pattern, which also gives a basic overview of the regular expression syntax supported by Java's regex engine.
1
newString = myString.replaceAll("[>\\[\\],-]", "");

2 Comments

sort of works but its removing a lot more than I want. it removed alm ost of all the data
@user521180: edited it a bit, try once again. Can you give us a data sample if it still doesn't work the way you wanted?
0

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.

Comments

0

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

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

1 Comment

If you don't be able to provide the whole link, better take it out of your question. Your answer could be seen to contain content of spam.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.