• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 286
  • 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

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