How to count the length of a String excluding the white spaces using replace() and length() methods
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
System.out.println("Length of the String: "+str.length());
//length of the String without white spaces
System.out.println("Length of String without spaces: "+
str.replace(" ", "").length());
}
}
#codingriver
#java
#programming
Видео How to count the length of a String excluding the white spaces using replace() and length() methods автора JavaScript Гениальность
Видео How to count the length of a String excluding the white spaces using replace() and length() methods автора JavaScript Гениальность
Информация
29 ноября 2023 г. 10:44:26
00:06:44
Похожие видео