Frequently Asked Java Program 23: Java Program to Remove Extra WhiteSpace Between Words In Given String

Remove extra white spaces between words. Only one white space is allowed between words. If more than one, remove it. If given string has leading or trailing white spaces, remove them as well. Do not use trim method. Write your own logic.

Example:

You entered with spaces: ”       Make       Selenium         Easy       ”
String after removing extra white space: “Make Selenium Easy”

This program was asked in Sapient.

Problem Solution:

  1. We have trim method which only removes leading and trailing whitespace/s of given string. We do not have any readymade methods to remove white spaces between words.
  2. We need to create a char array from given string which we need to iterate.
  3. If char at given index is a non space char, append it to result string.
  4. If char at given index is a white space, check if resultant string is zero in length. If length of resultant string is zero, go to next iteration. We need to go to next iteration as we do not want any leading white spaces in our resultant string. If length of resultant string is not zero, check if next index char is non space. If it is non space, append char to resultant string else to next iteration.

Java Program:

package StringPrograms;

import java.util.Scanner;

public class RemoveExtraWhiteSpacesBetweenWords {

        public static String removeExtraWhitespace(String str) {
                // Convert given string to a char array
                char[] c = str.toCharArray();

                // Output string variable
                String stringWithoutExtraWhitespaces = "";

                // For loop
                for (int i = 0; i < str.length(); i++) {

                        // If current char is a whitespace, check for next char
                        if (c[i] == ' ') {
                                
                                if(!(stringWithoutExtraWhitespaces.length()==0))
                                {
                                        // Check if next char is a non space. If yes, append in existing string.
                                        if (c[i + 1] != ' ') {
                                                stringWithoutExtraWhitespaces = stringWithoutExtraWhitespaces + c[i];
                                        }
                                }
                                
                        }
                        // Else append in new string stringWithoutExtraWhitespaces
                        else
                                stringWithoutExtraWhitespaces = stringWithoutExtraWhitespaces + c[i];

                }
                
                return stringWithoutExtraWhitespaces;

        }

        public static void main(String[] args) {

                // User input to trim spaces
                Scanner sc = new Scanner(System.in);
                System.out.println("Please enter the string to trim extra whitespaces:");

                String userInput = sc.nextLine();

                sc.close();

                System.out.println("You entered with spaces: \"" + userInput + "\"");

                System.out.println("String after removing extra white space: \"" + removeExtraWhitespace(userInput) + "\"");
        }
}

Output:

[java] Please enter the string to trim extra whitespaces: Make Selenium Easy You entered with spaces: ” Make Selenium Easy ” String after removing extra white space: “Make Selenium Easy” =================================================================== Please enter the string to trim extra whitespaces: Make selenium easy You entered with spaces: “Make selenium easy” String after removing extra white space: “Make selenium easy” ====================================================================

[/java]