how to remove extra white space in word in java code example
Example 1: how to remove all whitespace from string java
st = st.replaceAll("\\s+","")
Example 2: remove extra blank spaces from string in java
import java.util.Scanner;
class RemoveExtraSpace{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String input,output="";
char c;
int i,len;
System.out.println("ENTER A SENTENCE.");
input=sc.nextLine().trim();
len=input.length();
for(i=0;i<len;i++){
c=input.charAt(i);
output+=c;
if(c==' '){
while(input.charAt(i)==' '){
i++;
}
i--;
}
}
System.out.println(output);
}
}
Example 3: how to remove white space between numbers in a string in java
White space means blank space i