Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Last Updated: Mar 27, 2024

String trim()

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

One of the most widely used programming languages on the planet is Java. Learning Java strengthens fundamental computer science concepts and opens the door to various career opportunities.

In this blog, we will see one of the essential functions of Java  String trim() with an example.

String trim()

String trim() function

The trim() method removes whitespace from the provided string's leading (beginning) and trailing (ending) ends.

This procedure does not change the original string.

Syntax

Here is the syntax of the String trim() method −

string.trim()

 

The string is a String class object in this case.

Parameter

The trim() method accepts no parameters.

Return value

  • Removes whitespace from the beginning and end of a string.
  • If there is no whitespace at the beginning or end of the string, it returns the original string.

Example

Example

 

Before and After trim

Input: “  Coding Ninjas   “
Output: “Coding Ninjas”

Implementation 

Java Code

import java.util.*;
import java.lang.*;
import java.io.*;

class CodingNinjas
{
    public static void main (String[] args) throws java.lang.Exception
    {
        System.out.println("Before String trim() function : ");
    	String Str = "  Coding Ninjas   ";
    	System.out.println(Str);
    	String output = Str.trim();
       	
    	System.out.println("The output of the String trim() function : ");
    	System.out.println(output);
  
    }
}

 

Output

Before String trim() function :
Coding Ninjas   
The output of the String trim() function :
Coding Ninjas

Must Read: Java System Out Println

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Frequently Asked Questions

What is the purpose of the String trim() function in Java?

The purpose is to remove whitespace from both sides of a string.

How can we remove all white space characters from the string?

We can use the replaceAll() method in java to remove all the whitespace characters from a string.

What is the return type of String toCharArray()  in Java?

It returns a string with leading and trailing whitespace removed.

Conclusion

This article taught us about String trim() in Java and its implementation. The Java String trim() method removes whitespace from both sides of a string. We have also seen some examples to understand better how this method works.

Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem DesignMachine learning and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But if you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc; you must look at the problemsinterview experiences, and interview bundle for placement preparations.

Nevertheless, you may consider our paid courses to give your career an edge over others!

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!!

Thank you
Topics covered
1.
Introduction
2.
String trim() function
2.1.
Syntax
2.2.
Parameter
2.3.
Return value
2.4.
Example
2.5.
Implementation 
2.5.1.
Java Code
3.
Frequently Asked Questions
3.1.
What is the purpose of the String trim() function in Java?
3.2.
How can we remove all white space characters from the string?
3.3.
What is the return type of String toCharArray()  in Java?
4.
Conclusion