Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

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

Posted on 03/16/2025 By admin

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]

Uncategorized

Post navigation

Previous Post: #1. OAuth 2.0 Flow – How Does It Work?
Next Post: Frequently Asked Java Program 20: Java Program to Find Missing Alphabets in Given String

Related Posts

Selenium Framework 5: Understand Keyword Driven Framework in Selenium Uncategorized
August 18, 2017 – Make Selenium Easy Uncategorized
PackageTesng – Make Selenium Easy Uncategorized
Get Data From Non-Table HTML Tag WebTable Using Selenium WebDriver Uncategorized
#1. |Rest Assured Framework|Points To Consider Before Developing Test Automation Framework| Uncategorized
API Testing Tutorial Part 3 – Understand Terms URN , URL ,URI & API Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark