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.
package StringPrograms; import java.util.Scanner; public class TrimSpacesWithTrimMethod { 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:
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" =================================================
My name is Amod Mahajan and I am an IT employee with 4+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went throw so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning
By default a Test method is executed for all data set returned by DataProvider. The reason behind this is that…
In last post, we have learnt how can we make parameterized DataProvider method in TestNG to provide test data to…
"DataProvider" is an important feature provided by TestNG to provide required test data to test methods to run on. We…
Hello, DataProvider is an important functionality provided by TestNG to achieve Data driven testing or providing a set of different…
Hello Folks, As part of ongoing series on DataProvider, we will learn "How does Encapsulation ( in OOPS concept) help in…
Hello Folks, As part of our API Testing series, we will see "Sending first GET request in Postman". First of all…