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

  • Home
  • SEARCH
  • 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 7991455
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:17:42+00:00 2026-06-04T13:17:42+00:00

I try to replace some string in StringBuilder using the replace method, but unfortunately

  • 0

I try to replace some string in StringBuilder using the replace method, but unfortunately it running like the insert method.

Here is some code:

public class StringBuilderReplace {

    public static void main(String[] args) {
        StringBuilder builder = new StringBuilder();

        builder.append("Line 1\n");
        builder.append("Line 2\n");
        builder.append("Line 3\n");

        builder.replace(builder.indexOf("Line 2"), builder.indexOf("Line 2"), "Temporary Line\n");

        System.out.println(builder.toString());
    }
}

The result for this code:

Line 1
Temporary Line
Line 2
Line 3

What I want is:

Line 1
Temporary Line
Line 3

How to do this to get the result I want?

Update based on AljoshaBre answer

It works if I change the code like this one:

builder.replace(builder.indexOf("Line 2"), builder.indexOf("Line 3"), "Temporary Line\n");

But new problem occur, what if the next string (for this example Line 3) I don’t know the content?

  • 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-04T13:17:44+00:00Added an answer on June 4, 2026 at 1:17 pm

    It’s because you’re getting index of “Line 2” as starting index, which is the beginning of that line, and you do the same for the last index.

    I think you should do the following:

    public class StringBuilderReplace {
            public static void main(String[] args) {
                StringBuilder builder = new StringBuilder();
    
                builder.append("Line 1\n");
                builder.append("Line 2\n");
                builder.append("Line 3\n");
    
                String lineToReplace = "Line 1\n";
                int startIndex = builder.indexOf(lineToReplace);
                int lastIndex = startIndex + lineToReplace.length();
    
                builder.replace(startIndex, lastIndex, "Temporary Line\n");
                System.out.println(builder.toString());
            }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I try to replace few characters in some string 14/04/2010 17:12:11 and get, for
I try to remove some non-safe characters from a string but i believe i
I am using php and getting some utf8 string from Javascript. I try to
I'm trying to replace a string with another one by using stringByReplacingOccurrencesOfString, but for
I am using this tutorial to try to replace the default TitleBar with a
I am using sed -e s/foo/$bar/ -e s/some/$text/ file.whatever to replace a phrase in
I want to have a method like this on my DocumentFilter public void replaceUpdate(int
I'm trying to write an xml parser but seem to be running into some
I try to list file details using FtpWebRequest but very frequently it fails with
At input i have some string : today snowing know , here i have

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.