Length Of String In Java
Updated on Dec 30, 2024 | 4 min read | 7.1k views
Share:
For working professionals
For fresh graduates
More
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.
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.
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!
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.
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
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
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.
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.
A few companion methods associated with the Java string length are as follows:
Get Free Consultation
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
Top Resources