• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 282
  • Last Modified:

string manipulation question

how can i search a string for every occurance of "something" and replace each occurance with "something id="+counter

where counter is a variable that increments once for each occurance of "something"
0
wrynn
Asked:
wrynn
  • 3
  • 2
  • 2
  • +1
1 Solution
 
Mayank SAssociate Director - Product EngineeringCommented:
Guys, are we again posting full-code ;-) ?
0
 
Mayank SAssociate Director - Product EngineeringCommented:
You can use replaceAll ().
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
gauravkrtomarCommented:
U can use StringBuffer instead of String and iterate over the buffer and insert "id="+counter after each occurance of the string which u want to replace.
0
 
gauravkrtomarCommented:
:)
Thanks for the advice, people should keep all these things in mind, and also thanks for the consideration, people recently involved with the Expert-Exchange such things sometimes slips from the minds. :)
0
 
arun_kuttzCommented:
sorry...

pseudocode :

    initialize Stringbuffer (buffer) with substring of givenString till firstIndex of "something";
    initialize counter;
    while no-more-instances-of-"something" {
            nextindex = next indexOf "something" in given String;
            append buffer with substring of givenString(prev index till nextIndex)  of "something";
            if (nextindex is a valid index) {
                append-buffer-with ("somethingID="+counter)
                increment counter;
            }
        }

thanks for keeping us in check..
-KuTtZ
0
 
arun_kuttzCommented:
there is actually a mistake in the above pseudocode...

    initialize Stringbuffer (buffer) ;
    initialize counter;
    initialize prevIndex
    while no-more-instances-of-"something" {
            nextindex = next indexOf "something" in given String;
            append buffer with substring of givenString(prevIndex + length of "something" till nextIndex);
            if (nextindex is a valid index) {
                append-buffer-with ("somethingID="+counter)
                increment counter;
            }
            prevIndex = nextIndex;
        }

-KuTtZ
0
 
wrynnAuthor Commented:
thanks alot KuTtz
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now