trim() If you want to remove the whitespace before and after the string, use the ** trim ** method. However, white space in the middle of the character string is not subject to removal.
trim.java
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
String str = " Hello, World ";
//Remove whitespace before and after the string
System.out.println(str.trim());
}
}
result
Hello, World
replaceAll() If you want to remove all whitespace in the string, use the ** replaceAll ** method.
trim.java
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
String str = " Hello, World ";
//Remove all whitespace
System.out.println(str.replaceAll(" ",""));
}
}
result
Hello,World
Recommended Posts