Java Program to Trim Leading and Trailing Spaces from a String Last Updated : 27 Nov, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In Java, the trim() method is used to remove leading and trailing spaces from a string. This method does not remove spaces between words but it eliminates unnecessary spaces at the beginning and end of the string. The trim() method is defined under the String class in the java.lang package. It checks for Unicode value "\u002" which represents space characters.Program to Trim Leading and Trailing Spaces from a String in JavaIn this example, we will demonstrate how to use the trim() method to remove spaces from the beginning and end of a string.Example: java // Java program to remove leading and trailing spaces // using the trim() method public class RemoveSpaces { public static void main(String args[]) { String s1 = " Hello World "; System.out.println(s1); System.out.println(s1.trim()); String s2 = " Java Programming! "; System.out.println(s2); System.out.println(s2.trim()); } } Output Hello World Hello World Java Programming! Java Programming! Explanation: In the above example, the trim() method removed the leading and trailing spaces from both the strings s1 and s2 and it returns the new string without modifying the original, as strings in Java are immutable. Comment More infoAdvertise with us Next Article How to Remove all White Spaces from a String in Java? K kartik Improve Article Tags : Java Java Programs Java-Library Java-Strings Java-String-Programs +1 More Practice Tags : JavaJava-Strings Similar Reads Java Program to Replace All Line Breaks from Strings Given a string, write a Java program to replace all line breaks from the given String. Examples Input - Geeks For Geeks Output - Geeks For Geeks Line Break: A line break ("\n") is a single character that defines the line change. In order to replace all line breaks from strings replace() function can 2 min read Java - Remove White Spaces from String using Regex The Regex is an expression that is used for searching a required pattern or matching a required pattern or Manipulating the input based on requirements by using regex. This Regex is available in java.util.regex.* package in Java Programming. In this article, we will learn to remove the white spaces 2 min read How to Remove all White Spaces from a String in Java? In Java, to remove all white spaces from a String, there are several ways like using replaceAll() or manual iteration over characters. In this article, we will learn how to remove all white spaces from a string in Java.Example:We will use the replaceAll() method with the regex pattern "\\s" to remov 3 min read Java Program to Replace Multiple Characters in a String In this program, we will be discussing various methods for replacing multiple characters in String. This can be done using the methods listed below: Using String.replace() methodUsing replaceAll() method Using replaceFirst() method Method 1: Using String.replace() method This method returns a new st 3 min read Java Program to Add Characters to a String We will be discussing out how to add character to a string at particular position in a string in java. It can be interpreted as follows as depicted in the illustration what we are trying to do which is as follows: Illustration: Input: Input custom string = HelloOutput: --> String to be added 'Gee 4 min read Java Program to Separate the Individual Characters from a String The string is a sequence of characters including spaces. Objects of String are immutable in java, which means that once an object is created in a string, it's content cannot be changed. In this particular problem statement, we are given to separate each individual characters from the string provided 2 min read Like