Top community members
All Wiki Articles Create Wiki Article

Welcome to Dirask IT community! ❤ 💻
We are community of people that helps each other.

If you are beginner in IT field, you are more then welcome to ask questions, it will help you to learn faster. We are here to help you.

We are always beginner in something, we just need to remember it along the way.

there are no wrong questions - Ask Question

Java - replace last 4 chars in any string

0 contributions
14 points


I have:

String str = "123456";

I want:

String str = "12abcd";


We can replace / change last 4 x characters in any string in java with String.substring

String str = "123456";
System.out.println(str.substring(0, str.length() - 4) + "abcd"); // 12abcd

1. use substring
2. and add missing 4 x letters

Logic works for N x chars
Ensure that the length of string is longer then N


0 contributions

Checkout latest Findings & News:

Checkout latest questions:

Checkout latest wiki articles:

Hey 👋
Would you like to know what we do?
  • Dirask is IT community, where we share coding knowledge and help each other to solve coding problems.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Read more