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"
wrynnAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mayank SAssociate Director - Product EngineeringCommented:
Guys, are we again posting full-code ;-) ?
Mayank SAssociate Director - Product EngineeringCommented:
You can use replaceAll ().
OWASP: Forgery and Phishing

Learn the techniques to avoid forgery and phishing attacks and the types of attacks an application or network may face.

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.
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. :)
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
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
wrynnAuthor Commented:
thanks alot KuTtz
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.