Languages
[Edit]
EN

Java - replace last character in string

10 points
Created by:
Wallace
636

Problem

I would like to change string ABC into ABD - change the last letter from 'C' to 'D'.

Explanation with code, I have:

String str = "ABC";

I expected:

String str = "ABD";

Solutions

Example 1

String text = "ABC";

String substring = text.substring(0, text.length() - 1); // AB
String replaced = substring + "D";

System.out.println(text);     // ABC
System.out.println(replaced); // ABD

Steps:

  1. To make 'AB' from our string we use the String substring method with 2 parameters
    The first parameter is beginIndex and the second parameter is endIndex. EndIndex is the entire length of our string minus 1 letter to remove the last letter.
  2. We add the letter we want at the end of the string - in our case it's 'D'
  3. As expected we replaced the last letter from C to D.

Example 2

Let's visualize this problem on a string with digits. We want to replace the last letter of string '123' with digit '4' to have string '124'.

String text = "123";

String substring = text.substring(0, text.length() - 1); // 12
String replaced = substring + "4";

System.out.println(text);     // 123
System.out.println(replaced); // 124

Example 3

In the below example, we are gonna take a look at how to create our own utility method to help us to replace the last letter of the string. The below example have the utility method replaceLastLetter() which takes our string as the first parameter and our new letter as second parameter. As the result of the utility method, we get a string with replaced last letter. The below example takes the string 'ABC' and replaces the last letter 'C' into 'D' and the result is 'ABD'.

public class Example {

    public static String replaceLastLetter(String text, String newLetter) {
        String substring = text.substring(0, text.length() - 1); // ABC -> AB
        return substring + newLetter; // ABD
    }

    public static void main(String[] args) {
        String text = "ABC";
        String newLetter = "D";

        String replaced = replaceLastLetter(text, newLetter);

        System.out.println(text);     // ABC
        System.out.println(replaced); // ABD
    }
}

Example 4

This example is similar to example 3, but with the difference that we take character as the second parameter, not String type. In this example, we use the fact that in java we can concatenate strings with characters with the operator '+'.

The rest of the logic works in the same way.

public class Example {

    public static String replaceLastLetter(String text, char newLetter) {
        String substring = text.substring(0, text.length() - 1); // ABC -> AB
        return substring + newLetter; // ABD
    }

    public static void main(String[] args) {
        String text = "ABC";
        char newLetter = 'D';

        String replaced = replaceLastLetter(text, newLetter);

        System.out.println(text);     // ABC
        System.out.println(replaced); // ABD
    }
}

References

Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

Java - String (popular problems)

Java - replace last character in string
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join