Programs

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

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

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

 

 

Want to share this article?

Plan your Software Development Career Now!

Leave a comment

Your email address will not be published. Required fields are marked *

Our Popular Software Engineering Courses

Get Free Consultation

Leave a comment

Your email address will not be published. Required fields are marked *

×
Get Free career counselling from upGrad experts!
Book a session with an industry professional today!
No Thanks
Let's do it
Get Free career counselling from upGrad experts!
Book a Session with an industry professional today!
Let's do it
No Thanks