How to count the length of a String excluding the white spaces using replace() and length() methods

preview_player
Показать описание
How to find the length of a String excluding the white spaces using replace() and length() method

In the previous video, we saw how to use the Java String length() method in order to find out number of characters in a String. And we could also see that the length of the String is represented by the number of characters including the white spaces contained in that given String.
But in this video, we will see how to count the characters excluding the white spaces in the String.
This can be possible by using the String replace method together with the length method.
So we will replace all the white spaces with a null String
public class JavaExample {
public static void main(String[] args) {
String str = "hi guys this is a string";

//length of the String

//length of the String without white spaces
}
}

#codingriver
#java
#programming
Рекомендации по теме
Комментарии
Автор

This was the most easiest solution. I was trying to replace " " with "x" and then count out the spaces then trying to remove them from the String. It was going lengthy. This is short and better approach.

hameedferoz