Java strip whitespace from the beginning or the end of a string.
remove all white spaces at beginning and end of string java
String str = " hello lautturi java world "; str = str.trim();Source:www.lautturi.com
strip whitespace from the beginning of a string
public class Lautturi {
static String ltrim(String str) {
return (str == null)? null:str.replaceAll("^\\s+","");
}
public static void main(String[] args) {
String str = " hello lautturi java world ";
System.out.println(str);
str = ltrim(str);
System.out.println(str);
}
}
strip whitespace from the end of a string
public class Lautturi {
static String rtrim(String str) {
return (str == null)? null:str.replaceAll("\\s+$","");
}
public static void main(String[] args) {
String str = " hello lautturi java world ";
System.out.println(str);
str = rtrim(str);
System.out.println(str);
}
}