Blog_Banner_Asset
    Homebreadcumb forward arrow iconBlogbreadcumb forward arrow iconFull Stack Developmentbreadcumb forward arrow iconLength Of String In Java

Length Of String In Java

Last updated:
23rd Jun, 2023
Views
Read Time
4 Mins
share image icon
In this article
Chevron in toc
View All
Length Of String In Java

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.

Explore our Popular Software Engineering Courses

Ads of upGrad blog

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!

In-Demand Software Development Skills

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.

Source

Read our Popular Articles related to Software Development

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

Ads of upGrad blog

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

 

 

Profile

Pavan Vadapalli

Blog Author
Director of Engineering @ upGrad. Motivated to leverage technology to solve problems. Seasoned leader for startups and fast moving orgs. Working on solving problems of scale and long term technology strategy.

Explore Free Courses

Suggested Blogs

How to Rename Column Name in SQL
46648
Introduction We are surrounded by Data. We used to store information on paper in enormous file organizers. But eventually, we have come to store it o
Read More

by Rohan Vats

04 Mar 2024

Android Developer Salary in India in 2024 [For Freshers & Experienced]
900965
Wondering what is the range of Android Developer Salary in India? Software engineering is one of the most sought after courses in India. It is a reno
Read More

by Rohan Vats

04 Mar 2024

7 Top Django Projects on Github [For Beginners & Experienced]
50999
One of the best ways to learn a skill is to use it, and what better way to do this than to work on projects? So in this article, we’re sharing t
Read More

by Rohan Vats

04 Mar 2024

Salesforce Developer Salary in India in 2024 [For Freshers & Experienced]
908377
Wondering what is the range of salesforce salary in India? Businesses thrive because of customers. It does not matter whether the operations are B2B
Read More

by Rohan Vats

04 Mar 2024

15 Must-Know Spring MVC Interview Questions
34519
Spring has become one of the most used Java frameworks for the development of web-applications. All the new Java applications are by default using Spr
Read More

by Arjun Mathur

04 Mar 2024

Front End Developer Salary in India in 2023 [For Freshers & Experienced]
902189
Wondering what is the range of front end developer salary in India? Do you know what front end developers do and the salary they earn? Do you know wh
Read More

by Rohan Vats

04 Mar 2024

Method Overloading in Java [With Examples]
25538
Java is a versatile language that follows the concepts of Object-Oriented Programming. Many features of object-oriented programming make the code modu
Read More

by Rohan Vats

27 Feb 2024

50 Most Asked Javascript Interview Questions & Answers [2024]
3834
Javascript Interview Question and Answers In this article, we have compiled the most frequently asked JavaScript Interview Questions. These questions
Read More

by Kechit Goyal

26 Feb 2024

OOP Concepts and Examples That Every Programmer Should Know
25126
In this article, we will cover the basic concepts around Object-Oriented Programming and discuss the commonly used terms: Abstraction, Encapsulation,
Read More

by Rohan Vats

26 Feb 2024

Schedule 1:1 free counsellingTalk to Career Expert
icon
footer sticky close icon