Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

Frequently Asked Java Program 22: Java Program to Trim Whitespace From String Without Using Trim Method

Posted on 02/19/2025 By admin

Hello Folks,

This interview question was asked in Deloitte. 

Write a java program to Trim a given string without using inbuilt trim() method of String Class in Java.

“trim()” method of String class trims leading and trailing whitespace/s of given string. It does not remove whitespace between words. We need to write logic to do so.

Logic step by step:

  1. We need to find first index of non space char in given string from beginning. Say it First.
  2. We need to find first index of non space char in given string from end. Say it Last.
  3. Return a substring indexing from First till Last.

Detailed Explanation:

  1. Assume first char of given string is a non space char and set starting index of non space char as zero. Now check if it is really a non space char. If it it, finalize starting index as zero else increment index by one and repeat the same till you get a non space char.
  2. Assume last char of given string is a non space char and set last index of non space char as (Length of String-1). Now check if it is really a non space char. If it it, finalize end index as (Length of String-1) else decrement index by one and repeat the same till you get a non space char.
  3. Once Starting and Ending index are finalized, get the substring.

Java Program:

package StringPrograms;

import java.util.Scanner;

public class TrimSpacesWithoutTrimMethod {

        public static String trimSpace(String str) {

                // Assume 0th index is non-space character
                int startNonSpaceIndex = 0;

                // Assume last index is non-space character
                int endNonSpaceIndex = str.length()-1;

                // Store length in a variable
                int strLength = str.length();

                // Convert string into char array
                char[] val = str.toCharArray();

                // Increment startNonSpaceIndex by one until you really find a non space char
                while (startNonSpaceIndex < strLength && val[startNonSpaceIndex] == ' ') {
                        startNonSpaceIndex++;
                }

                // Decrement endNonSpaceIndex by one until you really find a non space char
                // Since we know starting index of a non space char, so we should not exceed
                // that which
                // is taking care by (startNonSpaceIndex < endNonSpaceIndex)
                while (startNonSpaceIndex < endNonSpaceIndex && val[endNonSpaceIndex] == ' ') {
                        endNonSpaceIndex--;
                }

                // Now you have actual starting and ending index of non space char in given
                // string.
                return str.substring(startNonSpaceIndex, endNonSpaceIndex+1);
        }

        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 spaces:");

                String userInput = sc.nextLine();

                sc.close();

                System.out.println("You entered with spaces: \"" + userInput+"\"");
                
                System.out.println("String after trimming space: \""+trimSpace(userInput) +"\"");
        }
}

Output:

[java] Please enter the string to trim spaces: Make Selenium Easy You entered with spaces: ” Make Selenium Easy ” String after trimming space: “Make Selenium Easy” ================================================= Please enter the string to trim spaces: Make Selenium Easy You entered with spaces: ” Make Selenium Easy ” String after trimming space: “Make Selenium Easy” =================================================

[/java]

Uncategorized

Post navigation

Previous Post: Protractor Tutorial 5 – Introduction & Installation of NodeJS
Next Post: Learn Selenium With Quiz – Basic Level 5

Related Posts

Bedsheet2 – Make Selenium Easy Uncategorized
Guidelines To Clear ISTQB Foundation Level Examination Uncategorized
Part 12: Usages Of Javascripts In Selenium : How To Type In Input Box Using Javascript Uncategorized
Frequently Asked Java Program 02: Java Program to Check if Any String is Palindrome Without Using Reverse Method Uncategorized
How To Upload Multiple Files In Selenium WebDriver – Java Uncategorized
October 22, 2017 – Make Selenium Easy 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