String

String length Java Example

Photo of Nikos MaravitsasNikos MaravitsasMarch 18th, 2014Last Updated: March 23rd, 2020
0 162 1 minute read

In this example, we are going to see how you can find out the length of a String in Java. By length string, we mean the number of Unicode characters orCode Units.

1. What is String in Computer Programming

In computer programming, a string is traditionally sequence of characters, either as a literal constant or as some kind of variable. The latter may allow its elements to be mutated and the length changed, or it may be fixed (after creation). A string is generally considered as a data type and is often implemented as an array data structure of bytes (or words) that stores a sequence of elements, typically characters, using some character encodingString may also denote more general arrays or other sequence (or list) data types and structures.

string length java

2. String length Java Example

String class has a very convenient method of doing so:length().

Here is how you can use it:

StringLengthExample.java

package com.javacodegeeks.core.string;public class StringLengthExample {    public static void main(String[] args) {        String s1 = "Java Code Geeks are awesome!";        int strLength  = s1.length();        System.out.println("The length of the string s1 : "+strLength);        System.out.println("The length of this string is : ".length());    }}

This will output:

The length of the string s1 : 2831

Take care when usinglength with words of languages that have multi-character letterslength will count the number of characters of theString, not lexicographical length.

There is also another way to count the number of characters in aString, but“unpaired surrogates within the text range count as one code point”:

StringFromatExample.java:

package com.javacodegeeks.core.string;public class StringLengthExample {    public static void main(String[] args) {        String s1 = "Java Code Geeks are awesome!";                System.out.println(s1.codePointCount(0, s1.length()));    }  }

This will output:28

This was a String length Java Example.

3. Download the Source Code

Download
You can download the full source code of this example here:String Length Java Example
Do you want to know how to develop your skillset to become aJava Rockstar?
Subscribe to our newsletter to start Rockingright now!
To get you started we give you our best selling eBooks forFREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to theTerms andPrivacy Policy

Thank you!

We will contact you soon.

Photo of Nikos MaravitsasNikos MaravitsasMarch 18th, 2014Last Updated: March 23rd, 2020
0 162 1 minute read
Photo of Nikos Maravitsas

Nikos Maravitsas

Nikos has graduated from the Department of Informatics and Telecommunications of The National and Kapodistrian University of Athens. During his studies he discovered his interests about software development and he has successfully completed numerous assignments in a variety of fields. Currently, his main interests are system’s security, parallel systems, artificial intelligence, operating systems, system programming, telecommunications, web applications, human – machine interaction and mobile development.
Subscribe
Notify of
guest
I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.

I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.