?
Solved

String - replace substring

Posted on 1999-11-12
6
Medium Priority
?
216 Views
Last Modified: 2010-04-16
How can one replace part of a string with another string?

e.g. Given a string "aaaaabbbbbbccccccdddddd", replace the "bbbbb" with "zzzzz" to make the string "aaaaazzzzzcccccddddd"


You can start with:
int i = fooStringl.indexOf ("bbbbb");

but then using replace() to replace one character at a time is inefficient.  

Converting it to StringBuffer doesn't seem to help, either.
There is insert(), but that's not quite it

0
Comment
Question by:omom
  • 3
  • 2
6 Comments
 
LVL 1

Expert Comment

by:bbangerter
ID: 2203715
Try:

String fooString = "aaaaabbbbbcccccddddd";
String barString = "bbbbb";
String replaceString = "zzzzz";
int fooLength = fooString.length ();
int barLength = barString.length ();
int index = fooString.indexOf (barString);

if (index != -1)
{
  String newString = fooString.substring (0, index) + replaceString + fooString.substring (index + barLength, fooLength);
}
else
{
  // barString does not exist inside of fooString - so nothing to replace
}

If there may be more than one instance of barString within fooString and you want to replace all instances of barString, then you will need to iterate through this code until index == -1



0
 

Expert Comment

by:wachi
ID: 2204080
From bbangerter 's comment. I think that code should be.

if (index != -1) {
  fooString  = fooString.substring (0, index) + replaceString +
      fooString.substring (index +  barLength, fooLength);
}

0
 

Author Comment

by:omom
ID: 2204145
Close enough, lock if you want to.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 

Author Comment

by:omom
ID: 2208870
I think bbangerter should get the points
0
 

Author Comment

by:omom
ID: 2208922
I think bbangerter should get the points
0
 
LVL 1

Accepted Solution

by:
bbangerter earned 150 total points
ID: 2208977
locked
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month4 days, 21 hours left to enroll

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question