Languages
[Edit]
EN

Java - replace last char in string

10 points
Created by:
JustMike
3497

Problem

I would like to change string ABC into ABD - change 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 String substring method with 2 parameters
    First parameter is beginIndex and 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 last letter from C to D.

Example 2

Let's visualize this problem on string with digits. We want to replace 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 below example we are gonna take a look at how to create own utility method to help us to replace last letter of the string. Below example have utility method replaceLastLetter() which takes our string as first parameter and our new letter as second parameter. As the result from the utility method we get string with replaced last letter. Below example takes string 'ABC' and replaces 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 second parameter, not String type. In this example we use the fact that in java we can concatinate strings with characters with operator '+'.

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

Hey 👋
Would you like to know what we do?
  • Dirask is a friendly IT community for learners, professionals and hobbyists to share their knowledge and help each other in extraordinary easy way.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.