java tutorial - Java Strings length() method - java programming - learn java - java basics - java for beginners

Learn Java - Java tutorial - Java strings length - Java examples - Java programs
Description
The length () method — The length () method returns the length of a string in Java. The length is equal to the number of 16-bit Unicode characters in the string.
Syntax
Syntax of this method:
public int length()
click below button to copy the code. By - java tutorial - team
Options
Detailed information about the parameters:
- not.
Return value
- In Java, length () returns the length of the sequence of characters represented by this object.
Example 1: Java string length definition
Below is an example of the length () method, which will help determine the length of a string.
import java.io.*;
public class Test {
public static void main(String args[]) {
String Str1 = new String("Welcome to wikitechy.com");
String Str2 = new String("Tutorials" );
System.out.print("String Length :" );
System.out.println(Str1.length());
System.out.print("String Length :" );
System.out.println(Str2.length());
}
}
click below button to copy the code. By - java tutorial - team
Output
String Length :24
String Length :9
Example 2: string length comparison
Also, using the length () method, you can not only learn the length of a string, but also compare the length of a string. Below is an example of how this can be done.
public class Example {
public static void main(String[] args)
{
String str = "wikitechy.com";
// Get the length of str.
int len = str.length();
System.out.println();
System.out.println("The string length of '"+str+"' is: "+len);
System.out.println();
}
}
click below button to copy the code. By - java tutorial - team
Output
The string length of 'wikitechy.com' is: 13