Removing the leading and trailing spaces from a string in Java
In this post we will give you information about Removing the leading and trailing spaces from a string in Java. Hear we will give you detail about Removing the leading and trailing spaces from a string in JavaAnd how to use it also give you demo for it if it is necessary.
To remove the leading trailing whitespace from a given string, we can use the built-in String.trim() method in Java.
In this example, we are removing the leading trailing whitespace from a place string.
public class Main{ public static void main(String[] args) { String place = " Manhattan "; String trimStr = place.trim(); System.out.println(trimStr); }}Output:
"Manhattan"
Similarly, you can also use the following regex pattern inside the replaceAll() method to trim it.
public class Main{ public static void main(String[] args) { String place = " Manhattan "; String trimStr = place.replaceAll("^\s+|\s+$", ""); System.out.println(trimStr); }}Hope this code and post will helped you for implement Removing the leading and trailing spaces from a string in Java. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs
