OneBite.Dev - Coding blog in a bite size

remove a character from a string in java

Code snippet on how to remove a character from a string in java

  String str = "Hello world!";
  String strNew = str.replace('l', '');

This code uses a String variable called ‘str’ which is set to the phrase “Hello world!“. Then we use the ‘replace’ method from the String class to create a new String variable. This ‘replace’ method takes two arguments, first is the character ‘l’ which we want to remove from the original string, and the second argument is an empty character which is used to replace it. Finally, the new variable ‘strNew’ is set to the result of the ‘replace’ method, which is our original string without the letter ‘l’.

java