Search code examples
javastringstring-formatting

How do I remove excessive spaces between characters in java String


I am pulling text from a file and for some reason it is formatted with extra whitespace between each character(ex: "H i , I a m a S t r i n g .").

Is there a way I can cleanly remove the excess white space something like

public String cleanString(Sting string){
    //do Something
    return cleanedString;
}
String oldString = "1  :  H i ,  I ' a m  a  S t r i n g .";

System.out.println(cleanString(oldString));

Output: "1: Hi, I'am a String."

Edit: By excessive spaces/white space I mean the extra space between every character in the string including the valid spaces.

I am replacing the "excessive space" with • so it is more visible String -> "H•e•l•l•o• •W•o•r•l•d" Imagine that the • is just white space or the "space" character. I would like to remove them.

The extra spaces are indeed spaces and not null characters, I double checked that.


Solution

  • We have to substitute space,not a space sequence with the second symbol(s) ( not a space ).

    There are more than one spaces between the words and one space between the letters in the word. With the regular expression oldString.replaceAll(" ([^ ]+)", "$1") we are removing any space that is followed by non space character. In that way only spaces is left between words and no spaces left between the letters in the word. Now we can normalize the spaces between words with the following expression: newString1.replaceAll("[ ]{2,}", " ")

    Below is the code that doing it:

      @Test
      void removeEmptySpaces() {
        final String oldString = "1  :  H i ,  I ' a m  a     S t r i n g .";
        final String expectedString = "1 : Hi, I'am a String.";
        // Trying to substitute all 'space,not a space' sequence with the second symbol(s)
        final String newString1 = oldString.replaceAll(" ([^ ]+)", "$1");
        final String newString = newString1.replaceAll("[ ]{2,}", " ");
        System.out.println(newString); // 1 : Hi, I'am a String.
        Assertions.assertEquals(expectedString, newString);
      }
    
      }