All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Java Trim String To Length

Last Updated : Mar 11, 2024

Java Trim String To Length

In this article we will show you the solution of java trim string to length, in this lesson, we will explore various Java techniques for trimming a String to a specific amount of characters.

We will begin by looking into how to accomplish this using the JDK itself.

After that, we will examine how to accomplish this using various well-known third-party libraries. Substring is a useful function provided by the String class.

We will now discuss the idea of how to trim string length in java with an example.

Step By Step Guide On Java Trim String To Length :-

public class TalkersCode
{
// main method
public static void main(String argvs[])
{
String str = " xyz ";
if((str.trim()).length() > 0)
{
System.out.println("The string contains characters other than white spaces \n");
}
else
{
System.out.println("The string contains only white spaces \n");
}
str = " ";
if((str.trim()).length() > 0)
{
System.out.println("The string contains characters other than white spaces \n");
}
else
{
System.out.println("The string contains only white spaces \n");
}
}
}
  1. It is declared that the TalkersCode class exists.
  2. It takes a String array parameter args as its only parameter when calling the main() method.
  3. This command declares a string object named str, which is assigned the value "xyz".
  4. The trimmed version of the string is checked to see if it contains any characters other than spaces using an if-else block.
  5. The output phrase "The string contains characters other than white spaces" is presented if the length of the trimmed string is larger than 0, which indicates that it contains characters other than white spaces.
  6. If the length of the trimmed string is less than 0, it means that there are no non-space characters, which is why the output statement "The string contains only white spaces" is printed.
  7. In this case, the string object is reassigned to the value "".
  8. For checking whether there are any characters other than spaces in the string, the same if-else block is used.
  9. According to whether non-space characters are present in the output, the same output statement will be printed.
  10. The main function and TalkersCode class have come to an end.

Conclusion :-

As a result, we have successfully learned how to trim string length in Java with an example.

The purpose of this article was to provide you with a variety of methods that can be used to truncate a String in Java to a particular number of characters.

Our discussion focused on using the JDK for this purpose.

I hope this article on java trim string to length helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Ashish

Ashish is a dynamic and motivated individual with a passion of programming and an experienced programmer having 3+ years of experience in various languages like Java, Python, HTML, CSS, JavaScript, jQuery and various frameworks like Bootstrap

Follow Ashish On Linkedin 🡪