View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All

Length Of String In Java

By Pavan Vadapalli

Updated on Dec 30, 2024 | 4 min read | 7.1k views

Share:

In simpler words, the number of characters that a string contains is called the length of a String in Java. If you want to find the number of characters of a String, an inbuilt method is available in Java known as length(). The length of a Java string and the Unicode characters of the String are the same.

The length() method 

An inbuilt length() method of the Java String class can be used when the length of a String has to be calculated. Strings are objects that are created with the help of the String class in Java, and the length() method belongs to this class and is a public member method. Hence, any type of string variable can use this method with the help of the . (dot) operator. The length() method calculates a String’s total number of characters.

Check out our free technology courses to get an edge over the competition.

Internal implementation

Source

Signature 

Mentioned below is the string length() method signature:

public int length()

Return Value 

This method returns the count of the total number of characters in Java.

Learn Software Development Courses online from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs or Masters Programs to fast-track your career.

Amid increased conversations around crypto and Blockchain technology, if you wish to educate yourself professionally on the topic, then upGrad’s Executive Post Graduate Programme in Software Development – Specialisation in Blockchain under IIIT- Bangalore is the right choice for you!

Coverage of AWS, Microsoft Azure and GCP services

Certification8 Months

Job-Linked Program

Bootcamp36 Weeks

Java String Length Method Example: 

The program given below will help to understand how to find the length of a String in Java. There are two Strings and the length of each String in Java will be calculated using the length() method.

Using Length() Method to Determine String Size Java

If you want to determine string size in Java, you can follow this method:

// Java program to illustrate
// how to get the length of String
// in Java using length() method
// Driver Class
public class Test {
    // main function
    public static void main(String[] args)
    {
        // Here str is a string object
        String str = "UpGrad"
        System.out.println("The size of "
                           + "the String is "
                           + str.length());
    }
}

Output:

The size of the String is 13

Comparing the Size of Two Strings

If you want to compare the length of string in Java, you need to follow this:

// Java program to illustrate how to check
// whether the length of two strings is
// equal or not using the length() method.
import java.io.*;
// Driver Class
class GFG {
    // main function
    public static void main(String[] args)
    {
        String s1 = "abc";
        String s2 = "xyz";
        // storing the length of both the
        // strings in int variables
        int len1 = s1.length();
        int len2 = s2.length();
        // checking whether the length of
        // the strings is equal or not
        if (len1 == len2) {
            System.out.println(
                "The length of both the strings are equal and is "
                + len1);
        }
        else {
            System.out.println(
                "The length of both the strings are not equal");
        }
    }
}

Output:

The length of both the strings are equal and is 3

Example of an Advanced Java String Length

You might have to find length of a string in Java in various scenarios. The code that you can use to identify whether a string is a palindrome is as follows:\

package com.mcnz.servlet;
/* Find the length of a Java String example */
public class JavaPalindromeCheckProgram {
  public static void main(String[] args) {
    boolean flag = palindromeCheck("amanaplanacanalpanama");
    System.out.println(flag);
  }
  /* This code uses the Java String length method in its logic */
  public static boolean palindromeCheck(String string){
    /* check if the Java String length is zero or one */
    if(string.length() == 0 || string.length() == 1) {
      return true;
    }
    if(string.charAt(0) == string.charAt(string.length()-1)) {
      return palindromeCheck(string.substring(1, string.length()-1));
    }
    return false;
  }
}

In this method, a huge variety of string classes like length(), substring(), and charAt() are used. 

How to Remove White Space from a Length of String in Java

Eliminating the white space is often crucial while manipulating a text or validating an input. The trim method is useful for achieving this. 

String javaString = " String length example ";
int stringSize= javaString.trim().length();
System.out.println(stringSize);
//This Java String length trim example prints out 21

In this example, the white spaces are excluded from calculating the length of string in Java

Companion Methods for Java String Length

A few companion methods associated with the Java string length are as follows:

  • trim(): Used for removing white space so that you can find length of string in Java
  • toUpperCase(): Makes all characters in a string uppercase
  • toLowerCase(): Makes all characters in a string lowercase
  • charAt(intindex): Can return a character to a particular position in the string
  • substring(intindex): Can return a subset of a string in Java

Pavan Vadapalli

900 articles published

Get Free Consultation

+91

By submitting, I accept the T&C and
Privacy Policy

India’s #1 Tech University

Executive PG Certification in AI-Powered Full Stack Development

77%

seats filled

View Program

Top Resources

Recommended Programs

upGrad

AWS | upGrad KnowledgeHut

AWS Certified Solutions Architect - Associate Training (SAA-C03)

69 Cloud Lab Simulations

Certification

32-Hr Training by Dustin Brimberry

upGrad KnowledgeHut

upGrad KnowledgeHut

Angular Training

Hone Skills with Live Projects

Certification

13+ Hrs Instructor-Led Sessions

upGrad

upGrad KnowledgeHut

AI-Driven Full-Stack Development

Job-Linked Program

Bootcamp

36 Weeks