18

How do i use replace(char, char) to replace all instances of character "b" with nothing.

For example:

Hambbburger to Hamurger

EDIT: Constraint is only JDK 1.4.2, meaning no overloaded version of replace!

Oh Chin Boon
  • 23,028
  • 51
  • 143
  • 215
  • 2
    You can't as 'nothing' isn't a char! Why can't you use `replace(String,String)`? – Sanjay Manohar Aug 10 '11 at 15:40
  • 1
    possible duplicate of [Java: remove all occurances of char from string](http://stackoverflow.com/questions/4576352/java-remove-all-occurances-of-char-from-string) – dogbane Aug 10 '11 at 15:44
  • Hi Dogbane, that is for Java 5 and above, which there will not be an overloaded version available for 1.4.2. – Oh Chin Boon Aug 10 '11 at 15:54

6 Answers6

22

There's also a replaceAll function that uses strings, note however that it evals them as regexes, but for replacing a single char will do just fine.

Here's an example:

String meal = "Hambbburger";

String replaced = meal.replaceAll("b","");

Note that the replaced variable is necessary since replaceAll doesn't change the string in place but creates a new one with the replacement (String is immutable in java).

If the character you want to replace has a different meaning in a regex (e.g. the . char will match any char, not a dot) you'll need to quote the first parameter like this:

String meal = "Ham.bur.ger";

String replaced = meal.replaceAll(Pattern.quote("."),"");
Pablo Fernandez
  • 103,170
  • 56
  • 192
  • 232
6

Strings are immutable, so make sure you assign the result to a string.

String str = "Hambbburger";
str = str.replace("b", "");

You don't need replaceAll if you use Java 6. See here: replace

MByD
  • 135,866
  • 28
  • 264
  • 277
3

Try this code....

public class main {
public static void main(String args[]){
    String g="Hambbburger.i want to eat Hambbburger. ";
    System.out.print(g);
    g=g.replaceAll("b", "");



      System.out.print("---------After Replacement-----\n");
      System.out.print(g);

}
}

output

Hambbburger.i want to eat Hambbburger. ---------After Replacement----- Hamurger.i want to eat Hamurger.

Gowthaman M
  • 8,057
  • 8
  • 35
  • 54
soumitra chatterjee
  • 2,268
  • 9
  • 26
  • 48
2
String text = "Hambbburger";
text = text.replace('b', '\0');

The '\0' represents NUL in ASCII code.

Gavin
  • 97
  • 8
  • @Tom It prints Hamurger. – Gavin Jul 13 '16 at 13:26
  • 1
    Then your console prints it differently than mine, because my console explicitly shows the `\0` characters (can't show it here, since SO removes them). So this solution might depends on the system where it will be run. Strange. – Tom Jul 13 '16 at 14:21
  • 2
    The answer should be removed because it is wrong `'\000'` does not represent nothing but the NUL character. Check the length of `text` before and after the replacement! It hasn't changed. – Guido Flohr Mar 23 '18 at 17:24
  • @GuidoFlohr Edited answer. It is true that the length didn't change and those NULs still exist. But it does the trick for what OP asked. – Gavin Apr 09 '18 at 01:51
  • 2
    @Andrew The OP asked to replace with *nothing* but a NUL byte is not nothin but just sometimes that looks like nothing (and there are a lot more unprintable characters than NUL, for example "\u200b" aka zero-width space). Add "System.out.println(text)" to your code, redirect the output to a file and open it in vi/vim. You will read "Ham^A^A^Aurger" because a NUL byte is CTRL-A. The original poster clearly meant "delete the b's" and your code does not do that. Sorry, if that sounds rude but your answer is confusing for beginners and really not helpful. – Guido Flohr Apr 10 '18 at 09:11
  • "... sometimes that looks ..." -> "... sometimes looks ..." – Guido Flohr Apr 10 '18 at 09:22
0

replaceAll in String doesnot work properly .It's Always recomend to use replace()

Ex:-

String s="abcdefabcdef";
s=s.replace("a","");
Gowthaman M
  • 8,057
  • 8
  • 35
  • 54
  • Hey, welcome to SO. replaceAll is fine, not sure where you are getting that from - do you have a specific buggy case to hand? Also you do not answer the question - the OP wants to *remove* a character, whereas there's no way to do this with replace(char, char). – pete23 May 29 '15 at 08:05
0
    String str="aabbcc";
    int n=str.length();
    char ch[]=str.toCharArray();

    for(int i=0;i<n-1;i++)
    {
        for(int j=i+1;j<n;j++)
        {
            if(ch[i]==ch[j])
            {
               ch[j]='*';
            }
        }
    }
    String temp=new String(ch);

    for(int i=0;i<temp.length();i++)
    {
        if(temp.charAt(i)!='*')
        System.out.print(temp.charAt(i));
    }