Make Selenium Easy

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

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:

 

Output:

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"
=================================================

#HappyCoding

Author: Amod Mahajan

A software Tester who is paid to judge products developed by others. Currently getting paid in American Dollars. Writing technical posts and creating YouTube videos are my hobbies.

2 thoughts on “Frequently Asked Java Program 22: Java Program to Trim Whitespace From String Without Using Trim Method

  1. public class MainClass {
    public static void main(String[] args) {
    String str = ” Make Selenium Easy “;
    int startIndex = 0;
    int lastIndex = 0;

    char[] charray = str.toCharArray();
    for (int i = 0; i 0; i–) {
    if (charray[i] != ‘ ‘) {
    lastIndex = i;
    break;}
    }
    System.out.println(str.substring(startIndex, lastIndex+1));
    }
    }

    1. public class MainClass {
      public static void main(String[] args) {
      String str = ” Make Selenium Easy “;
      int startIndex = 0;
      int lastIndex = 0;

      char[] charray = str.toCharArray();
      for (int i = 0; i 0; i–) {
      if (charray[i] != ‘ ‘) {
      lastIndex = i;
      break;}
      }
      System.out.println(str.substring(startIndex, lastIndex+1));
      }
      }

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.