replace only in substrings

  • Thread starter Vinicius Cubas Brand
  • Start date
V

Vinicius Cubas Brand

Hello. I am trying to find a method somewhere that makes replace in
substrings. For instance, I have seen that the String class have the
method replace(String regex, String replacement). I need a method that
do the replace only for a substring of this string, like something for
instance replace(String regex, String replacement, int startPosition,
int endPosition). So the replace would happen just inside the string
interval between startPosition and endPosition. Do you know if it is
some method implemented that does this operation? How would you do
this?

While writing this message I thought in solving this problem by
splitting the original string in three and just using the replace
operation in the middle string. However I need to do a lot of
replacements in a lot of sub-parts of the string, I think this will be
heavy to proccess.
 
D

Dave Miller

Vinicius said:
Hello. I am trying to find a method somewhere that makes replace in
substrings. For instance, I have seen that the String class have the
method replace(String regex, String replacement). I need a method that
do the replace only for a substring of this string, like something for
instance replace(String regex, String replacement, int startPosition,
int endPosition). So the replace would happen just inside the string
interval between startPosition and endPosition. Do you know if it is
some method implemented that does this operation? How would you do
this?

While writing this message I thought in solving this problem by
splitting the original string in three and just using the replace
operation in the middle string. However I need to do a lot of
replacements in a lot of sub-parts of the string, I think this will be
heavy to proccess.
If by "a lot" you mean something less than tens of millions, use your
solution. Manipulating Strings is relatively resource friendly and the
amount of resources saved by improving on your solution will be minimal
at best.
 
R

Roedy Green

Do you know if it is
some method implemented that does this operation? How would you do
this?

you would have to do something like his:

head = s.substring( ...);
middle = s.substring( ... );
tail = s.substring( ... );
middle = middle.replace( x, y);

s = head + middle + tail;

There is no way you are going to modify the middle of a string
directly because strings are immutable.

If you wanted method to pull that off more efficiently, look at how
replace is implemented, and add your bounds. Probably the game will
not be worth the candle.
 
S

Seamus

Hello. I am trying to find a method somewhere that makes replace in
substrings. For instance, I have seen that the String class have the
method replace(String regex, String replacement). I need a method that
do the replace only for a substring of this string, like something for
instance replace(String regex, String replacement, int startPosition,
int endPosition). So the replace would happen just inside the string
interval between startPosition and endPosition. Do you know if it is
some method implemented that does this operation? How would you do
this?

While writing this message I thought in solving this problem by
splitting the original string in three and just using the replace
operation in the middle string. However I need to do a lot of
replacements in a lot of sub-parts of the string, I think this will be
heavy to proccess.

Look into org.apache.commons.lang.StringUtils.{replace|overlay}()
http://commons.apache.org/lang/api/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,163
Latest member
Sasha15427
Top