Java string length method with an example

The String length method of Java

The length method of the String class returns total length of the specified string or the total number of characters in the given string.

An example of length method

Length method syntax

The syntax of the Java string length method is:


Where the Strex is a String class object.

Example of using string length method

In the following example of Java length method of the String class, we have created two string objects by two different methods. One is simply declaring a variable with the String keyword. While the second variable is created by using the new keyword.

After that, we have assigned values, that are two strings to these variable. Finally, the length method of Strings is used to get the length of two strings and printed on the screen by using the System.out.println statement of Java.

Experience this online

The output will be:

String length java=51

String length java=31


Also see Java string

Was this article helpful?

Related Articles

Leave A Comment?