How to count the length of a String excluding the white spaces using replace() and length() methods
Coding River
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 ... https://www.youtube.com/watch?v=NQpLHVN9zKY
8767643 Bytes