Answers for "how to find length of the string in java"

0

length method in java

public class LengthMethodDemo {

	public static void main(String[] args) {
		
        //declaring variables string1 & string2
		String string1 = "Java";
		String string2 = "Java Code";
	    
        //                 Java
        //count from left  1234  --> so length would be 4
		int lengthOfString1 = string1.length();  // 4
        //                 Java Code
        //count from left  123456789  --> so length would be 9
		int lengthOfString2 = string2.length();  // 9
		
		System.out.println("Length of String1 is : "+lengthOfString1);
		System.out.println("Length of String2 is : "+lengthOfString2);
		
		String string3="";
		System.out.println(string3.length());  //0
      
        String string4=null;
		System.out.println(string4.length());  //It will give NullPointerException
		
	}

}
Posted by: Guest on October-17-2021
0

length of string java

String txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
System.out.println("The length of the txt string is: " + txt.length());
Posted by: Guest on October-01-2021

Code answers related to "how to find length of the string in java"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language