Java substring last char

How to Get the Last Character of a String in Java

A string refers to the collection of characters used to process and store text data. The String class, which may be found in the “java.lang” package, serves as a representation of the string. In Java, a string is always an object of the type String. String objects are immutable, which means they cannot be changed after creation.

This tutorial will discuss the method related to getting the string’s last character in Java.

How to Get the Last Character of a String in Java?

In Java, you can get the string’s last character using:

We will now discuss each of the mentioned methods one by one!

Method 1: Getting Last Character of a String Using substring() Method

To get the string’s last character, you can use the Java “String” class “substring()” method. This method is primarily utilized to generate a substring by specifying the starting and ending indexes.

Syntax
The syntax for the substring() method is as:

Here, the substring() method takes two parameters as arguments: “startindex” and “endindex”.

We call this method to get the string’s last character as:

Here, “str.length() – 1” refers to the index of the last character of the string.

Example
We will create a String type variable named “str” and print its original value using the “System.out.println()” method:

Here, we will call the substring() method to get the last character of a string and save it in a String type variable named “lChar”. Lastly, we will print the resultant value on console:

Читайте также:  Javascript указать тип переменной

String lChar = str. substring ( str. length ( ) — 1 ) ;
System. out . println ( «Last word of the String is » + lChar ) ;

The given output signifies that we have successfully retrieved the last character of the specified string:

Let’s head toward the second method.

Method 2: Getting Last Character of a String Using charAt() Method

Another method to get the string’s last character is the “charAt()” method. It is a predefined method that returns the character value from the string using its index.

Syntax
The syntax for the method charAt() is:

It accepts the “index” of the character that needs to be fetched from the given string.

We call this method to get the string’s last character as:

Here, “str.length() – 1” indicates the last index of the character.

Example
In this example, we will create a character type variable “lChar” to store the last character of the “str” String by calling the “charAt()” method and passing the index of the last character as an argument:

At last, print the last character with the help of the “System.out.println()” method:

We have compiled all of the necessary instructions related to getting the string’s last character in Java.

Conclusion

For getting the string’s last character in Java, you can use two different methods: the substring() method and the charAt() method. The substring() method will return the character in the String type as a substring, while charAt() method will return the last character of a string in the character type. In this tutorial, we discussed the methods in detail for getting the string’s last character in Java.

About the author

Farah Batool

I completed my master’s degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.

Источник

Get the Last Character of a String in Java

Get the Last Character of a String in Java

  1. Get the Last Character of a String in Java Using substring()
  2. Get the Last Character of a String in Java Using charAt()
  3. Get the Last Character of a String in Java by Converting the String to a Char Array
Читайте также:  Java constructor method name

This tutorial will introduce how we can get the last character of the string in Java.

Get the Last Character of a String in Java Using substring()

In the below example code, the last character of exampleString is g . We will use a method of the String class called substring() that takes out a substring as its name suggests. The substring(startingIndex) method takes an argument which should be the index or position of the part of the string that we need. We only need the last character, so we pass exampleString.length() — 1 as the starting index.

Now we have the last character in the lastCharacter variable as a string. We need to have our character as a char so that we can use toCharArray() to convert the lastCharacter to an array of char and then get the only single character in that array.

public class LastCharString   public static void main(String[] args)    String exampleString = "This is a String";   String lastCharacter = exampleString.substring(exampleString.length() - 1);   char[] lastChar = lastCharacter.toCharArray();   System.out.println("Last char: "+lastChar[lastChar.length - 1]);  > > 

Get the Last Character of a String in Java Using charAt()

Instead of getting the last character as a string and then convert it to a char[] , we can directly get the last character in a string by using the chartAt() method of the String class. This method lets us get a specific single character at the specified index number.

exampleString.charAt(exampleString.length() — 1) is where we get the character located at the last position of exampleString .

public class LastCharString   public static void main(String[] args)    String exampleString = "This is a String";   char lastChar = exampleString.charAt(exampleString.length() - 1);   System.out.println("Last char: " + lastChar);  > > 

Get the Last Character of a String in Java by Converting the String to a Char Array

Another short method that we can use is to directly convert the exampleString into an array of char . It makes the code more concise and faster. We can use the toCharArray() method, which we used in this article’s first example, to convert the whole string to a char array. Then we get the last character by accessing the array with the last character’s index — lastCharArray.length — 1 .

public class LastCharString   public static void main(String[] args)    String exampleString = "This is a String";   char[] lastCharArray = exampleString.toCharArray();   char lastChar = lastCharArray[lastCharArray.length - 1];   System.out.println("Last char: " + lastChar);  > > 

Rupam Saini is an android developer, who also works sometimes as a web developer., He likes to read books and write about various things.

Related Article — Java Char

Related Article — Java String

Copyright © 2023. All right reserved

Источник

Get Last 4 Chars of a String in Java

Learn how to get last 4 characters of a String or simply any number of the last characters of a string in Java.

We may need to get the last 4 characters when we are dealing with customer-sensitive data such as phone numbers or SSNs. In this case, we need to display only the last 4 characters to mask the sensitive data.

To get a substring having the last 4 chars first check the length of the string. Suppose the string length is greater than 4 then use the substring(int beginIndex) method that returns the complete string from that specified beginIndex.

If the string length is less than 4, we can return the complete string as it is.

String input = "123456789"; String lastFourChars = ""; if (input.length() > 4) < lastFourChars = input.substring(input.length() - 4); >else

If data is in not in string form, first use the String.valueOf() method to convert it to String.

2. Using Apache Common’s StringUtils

 org.apache.commons commons-lang3 3.12.0 

The StringUtils.right() method gets the rightmost n characters of a String. If n characters are not available, or the String is null , the String will be returned without an exception. An empty String is returned if the argument is negative.

String input = "123456789"; String lastFourChars = StringUtils.right(input, 4); 

Источник

Оцените статью