Java program to remove all the white spaces from a string

Configurare noua (How To)

Situatie

In this program, our task is to remove all the white-spaces from the string. For this purpose, we need to traverse the string and check if any character of the string is matched with a white-space character or not. If so, use any built-in method like replace() with a blank.

Solutie

Pasi de urmat

public class removeWhiteSpace {
public static void main(String[] args) {

String str1=”Remove white spaces”;

//Removes the white spaces using regex
str1 = str1.replaceAll(“\\s+”, “”);

System.out.println(“String after removing all the white spaces : ” + str1);
}
}

Output:

String after removing all the white spaces: Removewhitespaces

Tip solutie

Permanent

Voteaza

(3 din 4 persoane apreciaza acest articol)

Despre Autor

Leave A Comment?