Java String Length() Method

Java String Length Method

What is string?

In Java, a string is an object that represents a sequence of characters. Strings are immutable, which means that once created, their value cannot be changed.

There are two ways to create a string in Java:

  1. Using a string literal: A string literal is a sequence of characters enclosed in double quotes.
  2. Using the String class: The String class has a constructor that allows you to create a string from an array of characters or from another string. 

To know more about String Class in java read the complete article.

String Length() method.

The length method is useful for various tasks, such as iterating through the characters of a string, or checking if a string is empty.

The length method is not the same as the length field of the String class. The length field is a final variable that holds the number of characters in the string, whereas the length method is a method that returns this value.

  • In Java, the length method is a built-in method of the String class that returns the length of a string, i.e., the number of characters in the string.

Syntax:

public int length()

Parameters:

regex - the regular expression to which this string is to be matched

Let’s look at a string-related Java program where the Java String length() Method is used to perform an operation on the given string.

Example: Java String length Method

public class Main {
  public static void main(String[] args) {
    String str = "Hello";
    int len = str.length();
    System.out.println("The length of the string is: " + len);
  }
}

Output

The length of the string is: 5

Example 2 : Java String Length Method

Run
public class Main {
  public static void main(String[] args) {
    String str = "PrepInsta!";

    // Get the length of the string
    int len = str.length();
    System.out.println("The length of the string is: " + len);  // The length of the string is: 13

    // Iterate through the characters of the string
    for (int i = 0; i < str.length(); i++) {
      char c = str.charAt(i);
      System.out.println("Character at index " + i + ": " + c);
    }
  }
}

Output

The length of the string is: 10
Character at index 0: P
Character at index 1: r
Character at index 2: e
Character at index 3: p
Character at index 4: I
Character at index 5: n
Character at index 6: s
Character at index 7: t
Character at index 8: a
Character at index 9: !

Example 3: how to use the length property:

Run

public class Main {
public static void main(String[] args) {
String str = "Hello, World!";
for (int i = 0; i < str.length(); i++) {
  char c = str.charAt(i);
  System.out.println("Character at index " + i + ": " + c);
}}
}

Output

Character at index 0: H
Character at index 1: e
Character at index 2: l
Character at index 3: l
Character at index 4: o
Character at index 5: ,
Character at index 6:  
Character at index 7: W
Character at index 8: o
Character at index 9: r
Character at index 10: l
Character at index 11: d
Character at index 12: !

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription