Pages

Footer Pages

Spring Boot

Java String API

Java Conversions

Kotlin Programs

Kotlin Conversions

Java Threads Tutorial

Java 8 Tutorial

Tuesday, May 28, 2019

Java String API length() Method Example

1. Java String length() Overview

In this tutorial, We'll learn how to find the length of the String using Java String API Length() Method. In other words, Counting the number of characters in String.

Knowing the string length is very important in iterating the string.

Note: All the blank spaces in string are counted as character.

Java String API length() Method Example



2. Length Syntax


public int length()

This is a public method. Directly can be accessed on instance.

The length is equal to the number of Unicode code units in the string.

2.1 Return type

int

2.2 Returns


length() method returns count of the sequence of characters represented.

3. String length example

3.1 Example program to find length of string


String str = "java-w3schools";
System.out.println(str.length());

Output:

14

3.2 Pin code validation with length() method

Assuming condition, if pin code length is 6 characters then it is valid. See the below code.

String pincode1 = "500001";

if (pincode1.length() == 6) {
 System.out.println("given pincode " + pincode1 + " is valid");
} else {
 System.out.println("given pincode " + pincode1 + " is not valid");
}

String pincode2 = "50000";

if (pincode2.length() == 6) {
 System.out.println("given pincode " + pincode2 + " is valid");
} else {
 System.out.println("given pincode " + pincode2 + " is not valid");
}

Output:

given pincode 500001 is valid
given pincode 50000 is not valid

3.3 Checking string empty with length


Taking a string that contains only white spaces like "      " but actually it does not have any value in it. Now, How to check the it is empty using length method.

First we need to remove white space from it then call length() method. If this returns length is 0 then it is empty.

String blankStr = "     ";
if(blankStr.trim().length() == 0) {
 System.out.println("Given String is empty");
}

Output:

Given String is empty

3.4 length example


String input1 = "java-w3schools";
int length1 = input1.length();
System.out.println("Input String length1: " + length1);

String input2 = "string length method";
int length2 = input2.length();
System.out.println("Input String length2: " + length2);

String input3 = "";
int length3 = input3.length();
System.out.println("Input String length3: " + length3);

Output:

Input String length1: 14
Input String length2: 20
Input String length3: 0

4. Internal Code


Now, We'll take a look at internal implementation code in Java 12.

public int length() {
    return value.length >> coder();
}

Here, Coder value is 1 for Latin character set and 0 for UTF 16 character set.   

value is a byte[] array.

It performs right shift operation on length with coder value.

5. Conclusion


In this article, We've learnt about Java String API length() method to calculate the number of characters in string.

And also seen example programs to validate length of strings. Explanation on internal code.

Example code snippets shown in this article is available on GitHub.


No comments:

Post a Comment

Please do not add any spam links in the comments section.