How to get the last character of a string in Java

How to get the last character of a string in Java

In this post we will give you information about How to get the last character of a string in Java. Hear we will give you detail about How to get the last character of a string in JavaAnd how to use it also give you demo for it if it is necessary.

we are going to learn about how to get the last character of a string in Java.

Consider, we have the following string.

String str = "google";

Now, we want to get the last character e from the above string.

Getting the last character

To access the last character of a string in Java, we can use the substring() method by passing string.length()-1 as an argument.

Here is an example that gets the last character e from the following string:

String str = "google";System.out.println( str.substring(str.length()-1));

Output:

"e"

Similarly, you can also get the last two characters of a string like this:

String str = "google";System.out.println( str.substring(str.length()-2));

Note: The extraction starts at str.length()-2 and ends upto last character index.

Hope this code and post will helped you for implement How to get the last character of a string in Java. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US