String Length Java

Hey guys!! Welcome to flower brackets blog. Here in this post we are going to string length 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 is specified by CharSequence interface and in turn returns length of characters. Here length is equal to the number of 16-bit unicode characters in the string.

Example: string length java

public class StringLength
{
   public static void main(String[] args)
   {
      // declaring string object
 
      String strExample = "CoreJava";
 
      // using string.length java
 
      int getLength = strExample.length();
      System.out.println("String Length is : " + getLength);
   }
}

Output:

string length java


Advanced – string length java

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.

string length 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.

Also Read – Learn Strings In Java

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 strExample = "CoreJava";
      char last = strExample.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 strExample “char” at seven?

Also Read – String Trim Method And Example In Java

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 strExample = "Ant";
      char last = strExample.charAt(strExample.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 strExample = "CoreJava";
      System.out.println("The last character is : " + strExample.substring(strExample.length() - 1, strExample.length()));
   }
}

Output:

The last character is : a

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

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

Also Read – Best Way To Remove Single Character From String

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 :: " + strExample.substring(strExample.length() - 1);

strExample 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 flower brackets if you have not already and if you find this post useful share it across social networks.

You May Also Like