I want to remove space happend after removing substring [closed]

Tags: ,



  public class RemoveString {
    public String removingOccurences(String input1, String input2) {

        String newString = input1.replaceAll(input2, " ");
        return newString;
    }
    public static void main(String[] args) {
        RemoveString removeString = new RemoveString();
        String newValue = removeString.removingOccurences("Hi john","o");
        System.out.println(newValue);
    }
}

Output is:

Hi j hn

I want to remove space between J and h

Answer

Currently you are replacing it with a whitespace

String newString = input1.replaceAll(input2, " ");

You can avoid this by reaplacing it with an empty string

String newString = input1.replaceAll(input2, "");

Also note the keyword public doesn’t start with a capital letter Public is public

Output will be:

Hi jhn 


Source: stackoverflow