Skip to content
Advertisement

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

  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

Advertisement

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 
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement