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

Java Free Online Course with Certification [2023]
52760
The PYPL Popularity of Programming Language Index maintains that Java is the second most popular programming language in the world, after Python.  Alt
Read More

by Rohan Vats

20 Sep 2023

Salesforce Developer Salary in India in 2023 [For Freshers & Experienced]
900202
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

20 Sep 2023

Literals In Java: Types of Literals in Java [With Examples]
3143
Summary: In this article, you will learn about Literals in Java. Literals in Java Integral Literals Floating-Point Literals Char Literals String Lit
Read More

by Rohan Vats

17 Sep 2023

Web Developer Salary in India in 2023 [For Freshers & Experienced]
899591
Wondering what is the range of Web Developer Salary in India? World Wide Web is fascinating. Even though it has been around for decades, the idea tha
Read More

by Rohan Vats

17 Sep 2023

Top 20 Project Ideas in C++ For Beginners [2023]
169448
Summary: In this article, you will learn the top project ideas in C++. Take a glimpse below Security Systems Car Rental System Dating Applications E
Read More

by Rohan Vats

14 Sep 2023

Top 20 Trending Android Project Ideas & Topics For Beginners [2023]
192228
Summary: In this article, you will learn the top 20 trending android project ideas & topics. Take a glimpse below. Android-based Function Gener
Read More

by Rohan Vats

13 Sep 2023

14 Exciting PHP Project Ideas & Topics For Beginners [2023]
171110
Summary: In this article, you will learn about 14 exciting PHP project ideas. Build a Clothes Recommendation System Customer Relationship Management
Read More

by Rohan Vats

13 Sep 2023

16 Exciting Javascript Project Ideas & Topics For Beginners [2023]
49057
JavaScript bridges the gap between the material and virtual worlds in the ever-converging worlds of technology and reality. JavaScript has become a vi
Read More

by Rohan Vats

12 Sep 2023

9 Exciting DBMS Project Ideas & Topics For Beginners [2023]
228965
Do you want to work on database projects but don’t know where to start? Then you’ve come to the right place. In today’s article, we’ll discuss some of
Read More

by Rohan Vats

12 Sep 2023

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