350

I can use this:

String str = "TextX Xto modifyX";
str = str.replace('X','');//that does not work because there is no such character ''

Is there a way to remove all occurrences of character X from a String in Java?

I tried this and is not what I want: str.replace('X',' '); //replace with space

1
  • 3
    Have you tried replacing single character Strings? Jan 1, 2011 at 23:46

13 Answers 13

571

Try using the overload that takes CharSequence arguments (eg, String) rather than char:

str = str.replace("X", "");
10
  • 2
    First argument is regular expression, sometimes it won't work as expected, especially if this string comes from user input.
    – vbezhenar
    Jul 4, 2012 at 8:50
  • 9
    @vsb: Not true. Both arguments of that particular overload are CharSequence. docs.oracle.com/javase/7/docs/api/java/lang/…
    – LukeH
    Jul 4, 2012 at 9:10
  • What to do in case the X is of type char?
    – KNU
    Mar 6, 2014 at 6:46
  • 7
    @Kunal: I guess you'd need to toString it first. So your code would look something like str = str.replace(yourChar.toString(), "");
    – LukeH
    Mar 6, 2014 at 10:01
  • Note you can use unicode escapes, e.g. no remove noncharacters str = str.replace("\uffff", ""); May 16, 2014 at 2:34
47

Using

public String replaceAll(String regex, String replacement)

will work.

Usage would be str.replace("X", "");.

Executing

"Xlakjsdf Xxx".replaceAll("X", "");

returns:

lakjsdf xx
3
  • 6
    Regex is probably overkill for this unless you're restricted to supporting Java 1.4 - since version 1.5 there's a replace overload that takes a simple CharSequence.
    – LukeH
    Jan 1, 2011 at 23:59
  • 3
    @LukeH, This is the decompiled source to String.replace. It's using regex. I agree that it regex feels heavy, but that's what is under the hood even for the accepted answer above. public String replace(CharSequence var1, CharSequence var2) { return Pattern.compile(var1.toString(), 16).matcher(this).replaceAll(Matcher.quoteReplacement(var2.toString())); }
    – Perry Tew
    Jun 20, 2016 at 12:33
  • 1
    Thankfully, that is not true anymore. It does not use regex anymore.
    – Nand
    Aug 17, 2020 at 16:12
29

If you want to do something with Java Strings, Commons Lang StringUtils is a great place to look.

StringUtils.remove("TextX Xto modifyX", 'X');
1
  • 1
    exactly what I looked for, probably because it just looks more clear than replace.
    – Line
    Sep 21, 2019 at 18:52
6
String test = "09-09-2012";
String arr [] = test.split("-");
String ans = "";

for(String t : arr)
    ans+=t;

This is the example for where I have removed the character - from the String.

3
  • 4
    This is very inefficient, especially compared with the accepted answer. Sep 27, 2012 at 17:13
  • 3
    I think this answer works, but the correct answer it is shorter and faster
    – evilReiko
    Sep 28, 2012 at 11:21
  • Just use replace instead to split. Also you should not use += operator in a loop.
    – Oskar
    Jul 30, 2021 at 8:56
3

Hello Try this code below

public class RemoveCharacter {

    public static void main(String[] args){
        String str = "MXy nameX iXs farXazX";
        char x = 'X';
        System.out.println(removeChr(str,x));
    }

    public static String removeChr(String str, char x){
        StringBuilder strBuilder = new StringBuilder();
        char[] rmString = str.toCharArray();
        for(int i=0; i<rmString.length; i++){
            if(rmString[i] == x){

            } else {
                strBuilder.append(rmString[i]);
            }
        }
        return strBuilder.toString();
    }
}
1
  • how would you do this if instead of x we had another string? Nice solution!
    – Mona Jalal
    Apr 11, 2016 at 4:02
3

Evaluation of main answers with a performance benchmark which confirms concerns that the current chosen answer makes costly regex operations under the hood

To date the provided answers come in 3 main styles (ignoring the JavaScript answer ;) ):

  • Use String.replace(charsToDelete, ""); which uses regex under the hood
  • Use Lambda
  • Use simple Java implementation

In terms of code size clearly the String.replace is the most terse. The simple Java implementation is slightly smaller and cleaner (IMHO) than the Lambda (don't get me wrong - I use Lambdas often where they are appropriate)

Execution speed was, in order of fastest to slowest: simple Java implementation, Lambda and then String.replace() (that invokes regex).

By far the fastest implementation was the simple Java implementation tuned so that it preallocates the StringBuilder buffer to the max possible result length and then simply appends chars to the buffer that are not in the "chars to delete" string. This avoids any reallocates that would occur for Strings > 16 chars in length (the default allocation for StringBuilder) and it avoids the "slide left" performance hit of deleting characters from a copy of the string that occurs is the Lambda implementation.

The code below runs a simple benchmark test, running each implementation 1,000,000 times and logs the elapsed time.

The exact results vary with each run but the order of performance never changes:

Start simple Java implementation
Time: 157 ms
Start Lambda implementation
Time: 253 ms
Start String.replace implementation
Time: 634 ms

The Lambda implementation (as copied from Kaplan's answer) may be slower because it performs a "shift left by one" of all characters to the right of the character being deleted. This would obviously get worse for longer strings with lots of characters requiring deletion. Also there might be some overhead in the Lambda implementation itself.

The String.replace implementation, uses regex and does a regex "compile" at each call. An optimization of this would be to use regex directly and cache the compiled pattern to avoid the cost of compiling it each time.

package com.sample;

import java.util.function.BiFunction;
import java.util.stream.IntStream;

public class Main {

    static public String deleteCharsSimple(String fromString, String charsToDelete)
    {
        StringBuilder buf = new StringBuilder(fromString.length()); // Preallocate to max possible result length
        for(int i = 0; i < fromString.length(); i++)
            if (charsToDelete.indexOf(fromString.charAt(i)) < 0)
                buf.append(fromString.charAt(i));   // char not in chars to delete so add it
        return buf.toString();
    }

    static public String deleteCharsLambda(String fromString1, String charsToDelete)
    {
        BiFunction<String, String, String> deleteChars = (fromString, chars) -> {
            StringBuilder buf = new StringBuilder(fromString);
            IntStream.range(0, buf.length()).forEach(i -> {
                while (i < buf.length() && chars.indexOf(buf.charAt(i)) >= 0)
                    buf.deleteCharAt(i);
            });
            return (buf.toString());
        };

        return deleteChars.apply(fromString1, charsToDelete);
    }

    static public String deleteCharsReplace(String fromString, String charsToDelete)
    {
        return fromString.replace(charsToDelete, "");
    }


    public static void main(String[] args)
    {
        String str = "XXXTextX XXto modifyX";
        String charsToDelete = "X";  // Should only be one char as per OP's requirement

        long start, end;

        System.out.println("Start simple");
        start = System.currentTimeMillis();

        for (int i = 0; i < 1000000; i++)
            deleteCharsSimple(str, charsToDelete);

        end = System.currentTimeMillis();
        System.out.println("Time: " + (end - start));

        System.out.println("Start lambda");
        start = System.currentTimeMillis();
        for (int i = 0; i < 1000000; i++)
            deleteCharsLambda(str, charsToDelete);

        end = System.currentTimeMillis();
        System.out.println("Time: " + (end - start));

        System.out.println("Start replace");
        start = System.currentTimeMillis();

        for (int i = 0; i < 1000000; i++)
            deleteCharsReplace(str, charsToDelete);

        end = System.currentTimeMillis();
        System.out.println("Time: " + (end - start));
    }
}
6
  • If the lambda function is called as it is intended to do, the timing is the following (nobody wraps a lambda function into a member function). Furthermore Your deleteCharsReplace() is wrong implemented: it replaces one String "XYZ" and not as required 'X','Y' and 'Z' what fromString.replace("X", "").replace("Y", "").replace("Z", ""); would need. Now we get the correct timing: Start simple Time: 759 | Start lambda Time: 1092 | Start deleteCharsLambda() Time: 1420 | Start replace corrected Time: 4636
    – Kaplan
    Nov 20, 2019 at 11:11
  • "nobody wraps a lambda function into a member function" - except for the purpose of calling it in a benchmark scenario so that it is consistent with the way the other implementations are called.
    – Volksman
    Nov 21, 2019 at 22:10
  • I just realized that the OP asked about removing all occurrences of a single character but your answer changed the scope to deal with a set of characters. The "accepted" answer implementation that I used does not and was never intended to cater for multiple characters. So I have updated the above benchmark to reflect this and the benchmark times. BTW if you want to increase the scope to support multiple characters calling replace multiple times is costly. Better to switch to a single call to replaceAll("[XYZ]", "")
    – Volksman
    Nov 21, 2019 at 22:26
  • The function as shown in the solution is only inited once when called. To wrap the function definiton additionally to the function call into the member function has the only effect to distort the benchmark.
    – Kaplan
    Nov 22, 2019 at 13:06
  • It's virtually impossible to properly benchmark quick duration methods by making a single call as the variance of each call is so high. So benchmarking normally involves many repeated calls to the same method and then total time is evaluated to compare with total times of the alternatives (or to calculate an average if required)..
    – Volksman
    Nov 24, 2019 at 21:29
2

I like using RegEx in this occasion:

str = str.replace(/X/g, '');

where g means global so it will go through your whole string and replace all X with ''; if you want to replace both X and x, you simply say:

str = str.replace(/X|x/g, '');

(see my fiddle here: fiddle)

1
  • I guess this might work, but the correct answer executes faster and shorter, it's always better to avoid RegEx as much as possible as it's known to be slower than other methods
    – evilReiko
    Sep 17, 2013 at 10:45
2

Use replaceAll instead of replace

str = str.replaceAll("X,"");

This should give you the desired answer.

1
  • 1
    replace ends up using replaceAll. Look into the implementation. This is how String#replace is implemented: return Pattern.compile(target.toString(), Pattern.LITERAL).matcher( this).replaceAll(Matcher.quoteReplacement(replacement.toString()));
    – Molasses
    Jun 27, 2018 at 19:14
1

You will need to put the characters needs to be removed inside the square brackets during the time of replacement. The example code will be as following:

String s = "$116.42".replaceAll("[$]", "");
0

here is a lambda function which removes all characters passed as string

BiFunction<String,String,String> deleteChars = (fromString, chars) -> {
  StringBuilder buf = new StringBuilder( fromString );
  IntStream.range( 0, buf.length() ).forEach( i -> {
    while( i < buf.length() && chars.indexOf( buf.charAt( i ) ) >= 0 )
      buf.deleteCharAt( i );
  } );
  return( buf.toString() );
};

String str = "TextX XYto modifyZ";
deleteChars.apply( str, "XYZ" ); // –> "Text to modify"

This solution takes into acount that the resulting String – in difference to replace() – never becomes larger than the starting String when removing characters. So it avoids the repeated allocating and copying while appending character-wise to the StringBuilder as replace() does.
Not to mention the pointless generation of Pattern and Matcher instances in replace() that are never needed for removal.
In difference to replace() this solution can delete several characters in one swoop.

7
  • 1
    Lambdas/Functional Programming is very hip right now but using it to create a solution that is 10x longer than the chosen answer can't be justified IMHO, hence the down vote.
    – Volksman
    Nov 18, 2019 at 6:17
  • str.replace("…", "") instantiates private Pattern(…) and then on the generated pattern calls public String replaceAll(String repl). So the following function-calls happened: return Pattern.compile(target.toString(), Pattern.LITERAL).matcher( this).replaceAll(Matcher.quoteReplacement(replacement.toString())); – see Sal_Vader_808 comment. All in all c.a. 3 times longer than my hip lambda solution. And here it is nicely explained why my hip lambda solution is also faster: Why is Java's String::replace() so slow?
    – Kaplan
    Nov 18, 2019 at 15:59
  • in own thing: If it were really about the size of the solution, some other solutions twice as big or the solutions that require an external library would be more suitable candidates for criticism. A language extension that has been part of the language for years since Java 8 is not really hip. A general problem with the scoring system is, that the time factor weighs more heavily than the quality of a solution. As a result, the more up-to-date and sometimes even better solutions are increasingly being found in the back third.
    – Kaplan
    Nov 19, 2019 at 13:38
  • I was referring to 10x longer in terms of code not execution speed. Anything that compiles a regex pattern each time it's called can be much slower. You would really need to cache the compiled matcher and reuse if using such regex at high frequency (OP does not say what scenario it is used it - might be a rare scenario to clean up data from a form submission or could be used in a tight loop being called 1000s of times a second).
    – Volksman
    Nov 19, 2019 at 20:32
  • In regard to performance concerns i added a new answer which runs a quick benchmark on a variety of the answers provided. If the OP is doing this operation frequently then they should avoid the String.replace() option as the repeated recompilation of the regex pattern under the hood is very costly.
    – Volksman
    Nov 19, 2019 at 21:53
0

…another lambda
copying a new string from the original, but leaving out the character that is to delete

String text = "removing a special character from a string";

int delete = 'e';
int[] arr = text.codePoints().filter( c -> c != delete ).toArray();

String rslt = new String( arr, 0, arr.length );

gives: rmoving a spcial charactr from a string

-1
package com.acn.demo.action;

public class RemoveCharFromString {

    static String input = "";
    public static void main(String[] args) {
        input = "abadbbeb34erterb";
        char token = 'b';
        removeChar(token);
    }

    private static void removeChar(char token) {
        // TODO Auto-generated method stub
        System.out.println(input);
        for (int i=0;i<input.length();i++) {
            if (input.charAt(i) == token) {
            input = input.replace(input.charAt(i), ' ');
                System.out.println("MATCH FOUND");
            }
            input = input.replaceAll(" ", "");
            System.out.println(input);
        }
    }
}
1
  • input = "deletes all blanks too"; gives "deletesalllankstoo"
    – Kaplan
    Oct 2, 2019 at 21:46
-4

You can use str = str.replace("X", ""); as mentioned before and you will be fine. For your information '' is not an empty (or a valid) character but '\0' is.

So you could use str = str.replace('X', '\0'); instead.

1
  • 10
    this is incorrect. '\0' will produce an actual null character. str.replace('X', '\0') is equivalent to str.replace("X", "\u0000") which is not at all what the OP wanted
    – Andrey
    Jul 26, 2012 at 18:00

Not the answer you're looking for? Browse other questions tagged or ask your own question.