How to get last character of string in Java?

Getting the Last Character of a String in Java

Introduction

In Java, strings are sequences of characters that are enclosed in quotes or enclosed within a character array. When working with strings, you often need to access specific characters or extract information from them. One common task is to get the last character of a string. In this article, we will explore how to achieve this in Java.

Direct Answer: Using the lastIndexOf() Method

The lastIndexOf() method is a built-in method in Java that returns the index of the last occurrence of a specified character or substring in a string. Here’s how to use it:

  • String str = "Hello, World!";
  • char lastChar = str.charAt(str.length() - 1);
  • System.out.println(lastChar);

In this example, str.length() - 1 returns the index of the last character in the string. The charAt() method is used to access the character at a specific index.

Important Points:

  • The lastIndexOf() method returns the index of the last occurrence of the specified character or substring. If no such character or substring is found, it returns -1.
  • The index is 0-based, meaning the first character is at index 0.

Alternative Method: Using the indexOf() Method

The indexOf() method is another built-in method in Java that returns the index of the first occurrence of a specified character or substring in a string. Here’s how to use it:

  • String str = "Hello, World!";
  • char lastChar = str.indexOf('z');
  • System.out.println(lastChar);

In this example, str.indexOf('z') returns the index of the first occurrence of the character ‘z’ in the string. The index is 0-based, meaning the first character is at index 0.

Important Points:

  • The indexOf() method returns the index of the first occurrence of the specified character or substring. If no such character or substring is found, it returns -1.
  • The index is 0-based, meaning the first character is at index 0.

Using Regular Expressions

Regular expressions (regex) are a powerful tool for matching patterns in strings. You can use regex to extract the last character of a string. Here’s an example:

  • String str = "Hello, World!";
  • String lastChar = str.replaceAll("[^a-zA-Z0-9]", "") + str.charAt(str.length() - 1);
  • System.out.println(lastChar);

In this example, str.replaceAll("[^a-zA-Z0-9]", "") removes all non-alphanumeric characters from the string. The + operator is used to concatenate the remaining characters with the last character.

Important Points:

  • The replaceAll() method replaces all occurrences of a specified pattern in a string. The pattern is a regular expression.
  • The + operator is used to concatenate the remaining characters with the last character.

Using a Loop

You can also use a loop to extract the last character of a string. Here’s an example:

  • String str = "Hello, World!";
  • char lastChar = str.charAt(str.length() - 1);
  • while (lastChar != '') {
  • System.out.println(lastChar);
  • lastChar = str.charAt(str.length() - 1);
  • }
  • System.out.println(lastChar);

In this example, the loop continues until the last character is ‘’, which is the null character.

Important Points:

  • The loop continues until the last character is ‘’, which is the null character.
  • The loop uses a while loop to iterate over the characters in the string.

Conclusion

In this article, we explored how to get the last character of a string in Java using the lastIndexOf() method, the indexOf() method, and regular expressions. We also discussed using a loop to extract the last character of a string. By understanding these methods and techniques, you can efficiently access and manipulate strings in your Java programs.

Table:

Method Description Parameters
lastIndexOf() Returns the index of the last occurrence of a specified character or substring. str, char
indexOf() Returns the index of the first occurrence of a specified character or substring. str, char
replaceAll() Removes all occurrences of a specified pattern in a string. str, String
charAt() Returns the character at a specific index in a string. str, int

Code Snippets:

  • String str = "Hello, World!";
  • char lastChar = str.charAt(str.length() - 1);
  • System.out.println(lastChar);

  • String str = "Hello, World!";
  • String lastChar = str.replaceAll("[^a-zA-Z0-9]", "") + str.charAt(str.length() - 1);
  • System.out.println(lastChar);

  • String str = "Hello, World!";
  • while (lastChar != '') {
  • System.out.println(lastChar);
  • lastChar = str.charAt(str.length() - 1);
  • }
  • System.out.println(lastChar);

Unlock the Future: Watch Our Essential Tech Videos!


Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top