How To Find Length Of A String In Java

Sharing is healthy !!

Here in this post we are going to demonstrate how to find length of a string in java.

The string class is a powerful class in java. Here we are going to look at the humble method of the string class called length.

And to do that I have created a string called “CoreJava”.

To find out the length of the string in java we have length() method which specified by CharSequence interface and in turn returns length of characters.

Example: How To Find Length Of A String In Java

Also Read – How To Open Notepad in Java

public class String_Length {

public static void main(String[] args) {
 
// declaring string object
 
String str_Example = "CoreJava";
 
// using length() method
 
int get_Length = str_Example.length();
System.out.println("String Length is : " + get_Length);

}

}

Output:

How To Find Length Of A String In Java


advanced

We know that the length of “CoreJava” is eight. But if we look at the index of “CoreJava” it would be from zero to seven.

You will notice that the index is slightly different from the length.

How To Find Length Of A String In Java

The index is always going to start at zero whereas the length is always going to start counting at one.

Well where is this going to make a difference? It’s going to make a difference in the last character. The last characters index is going to be seven.

Whereas the length is going to be eight. So we can say that the length is always going to be one greater than the highest index of the string.

And the index is always going to be one less than the length of the string.

So let’s take the example of the string “CoreJava” and find the last letter in “CoreJava” or start from the end of “CoreJava”.

How would we do that?

We could do it by simply finding the last character which is at the index seven and if we were to print this out now it would be like this,

public class StringLength
{
public static void main(String[] args)
{
String str_Example = "CoreJava";
char last = str_Example.charAt(7);
System.out.println("The last character is : " + last);
}
}

Output:

The last character is : a

But what if I didn’t tell you what word I’m going to get or i used a different word. In this case I have used the word “Ant”.

Would it work still for the above word “Ant” in the statement str_Example “char” at seven?

No!! It would give an index out of bounds. Because “Ant” does not have an index of seven.

I’m going to show you a way that you can use length to determine what the last letter is of any string no matter what string it is.

Here’s the code

public class StringLength
{
public static void main(String[] args)
{
String str_Example = "Ant";
char last = str_Example.charAt(str_Example.length() - 1); System.out.println("The last character is : " + last);
}
}

Output:

The last character is : t

We are simply going to subtract one from the length. This is going to be true for any word that I use inside above program.

This can be a useful tool for finding the end of a string.


different ways

Next let us learn different ways to find the last letter using different methods.

Now we will use “substring”. Here I have the code

public class StringLength
{
public static void main(String[] args)
{
String str_Example = "CoreJava";
System.out.println("The last character is : " + str_Example.substring(str_Example.length() - 1,str_Example.length()));
}
}

Output:

The last character is : a

The word substring and the first parameter would be str_Example dot length minus one. And the second parameter would be str_Example dot length.

Because str_Example dot length is always going to be one more than the index that we are looking for.

You have to remember the second parameter of substring is exclusive. So if you wouldn’t include this it would again give us the last letter is “a”.

Then one more example would be

System.out.println("The last character is :: " + str_Example.substring(str_Example.length() - 1);

str_Example dot length minus one. Well it happens to work in this case because the way the overloaded substring method works is, if you put one parameter inside of there it’s going to start there and go to the end.

conclusion:

Although length provides a simple tool it is also a powerful tool inside of the string class, if utilized correctly.

I hope you have understood how to find length of a string in java with an example.

You can subscribe to my blog if you have not already and if you find this post useful share it across social networks.

Sharing is healthy !!

Leave a Reply

Your email address will not be published.