Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9218205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:48:30+00:00 2026-06-18T02:48:30+00:00

Say I have the string 123456789 . I know that I want to replace

  • 0

Say I have the string 123456789. I know that I want to replace indexes 3-5 of the string. (4, 5, and 6). Keep in mind that the replacement probably isn’t 3 characters, I might want to end up with 123foobar789.

What’s the best way to do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T02:48:31+00:00Added an answer on June 18, 2026 at 2:48 am

    Firstly, you cannot do it1, since a String is immutable in java.

    However, you can create a new String object with the desired value, using the String#substring() method (for example):

    String s = "123456789";
    String newString = s.substring(0, 3) + "foobar" + s.substring(3+3);
    System.out.println(newString);
    

    If you do want to achieve it efficiently, you could avoid creating some intermediate strings used by the concatinating and substring() method.

    String s = "123456789";
    StringBuilder sb = new StringBuilder();
    char[] buff = s.toCharArray();
    sb.append(buff , 0, 3).append("foobar");
    sb.append(buff,3+3,buff.length -(3+3));
    System.out.println(sb.toString());
    

    However, if it is not done in a very tight loop – you should probably ignore it, and stick with the first and more readable solution.


    (1) not easily anyway, it can be done with reflection – but it should be avoided.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

lets say i have a number string 1234567890 and out of that i want
Let's say we have a string: abcbcdcde I want to identify all substrings that
Say I have a string, ab . I want to replace a with b
Say i have a string that i need to evaluate in javascript such as
Say I have the string 1,2,3,4,5 and I want to convert this to an
Let's say I have String Table that have a few strings (like mother, father,
Say I have a string like .vcproj; .csproj;*.sln and I want to show all
I have an xml in the form of a string. Let's say that the
Let's say have a string... String myString = my*big*string*needs*parsing; All I want is to
Okay. Say I have string '193' And I want to remove the last numbers

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.