Replaces all occurnces of one String with another String in a given source

Hi all I had found that there is a lot of interest int this topic. there is a little piece of code that I wrote for myself recently, so I fgigured that it might be helpful for some...
This method will scan through a string "source", find all of the occurances of a String "before" and replace them with the "after".
Enjoy :-)
public static String replace(String source, String before, String after)
StringBuffer sb = new StringBuffer(source);
int startpos = source.indexOf(before);
int endpos = startpos + before.length();
int i = startpos;
while( i > -1 )
     if( startpos > -1 && endpos <= source.length() )
     source = sb.replace(startpos, endpos, after).toString();
     int lastReplace = source.lastIndexOf(after) + after.length();
     if(lastReplace <= source.length())
          startpos = source.substring(lastReplace, source.length() ).indexOf(before);
               endpos = startpos + before.length();
               i = startpos;
     else
          i = source.length();
     else
          i = -1;
return source;
Oh yeah if you want, you can visit my site http://www.infobrokery.com it is where this code is used to replace the text URL with html.

Since 1.4 the String class has a replaceAll method ...

Similar Messages

  • Compliance Calibrator 4.0: replace whole ruleset on one system with another

    Hi All,
    We have two R/3 environments (one's at 4.7 the other is 4.6c).  Both are running CC 4.0.  What's the best way to replace the whole SOD ruleset on the 4.6c system with the one from our 4.7 system.  When I say whole ruleset I mean all the risk, function, and business process area tables then regenerating all the rules.  I don't care if the 9-digit risk ID's are identical in each system. And I don't want to copy any of the mitigation information to the other system.
    I looked at the download/upload utility and it seems to deal with the generated rules.  The text files it downloaded do not appear to contain anything resembling the function or risk tables.  I assume we want to do more of an export/import activity similar to an upgrade conversion or new install (but using the risks and functions from our other system instead of the default set supplied by Virsa.) I can extract the tables I want (/virsa/bussproct, /virsa/funtiont, /virsa/functtcd, /virsa/functobj, /virsa/risks, etc.) and manipulate them into whatever is needed.  But I'm not sure how to delete the existing tables contents and import my data from the other system.
    Thanks in advance,
    Doug

    Hi Doug,
    How did you replace rule sets in one system from other system? Please explain if you have resolved your issue. I am doing same.

  • Replacing all instances of a swatch with another swatch

    Ok, this really has to be easy but I can't seem to find a way to accomplish this.
    I have a swatch that's used throughout my document. Now I want to change all objects that use this swatch to use another swatch. How do I accomplish this?

    Thanks Dave and Ken!
    Dave, I tried redefining the swatch. But since it was a Pantone color I tried to redefine to (and that color swatch was already defined because I'd imported an object with that Pantone color) it wouldn't work.
    Deleting the swatch and replacing it worked like a charm however. Thanks again!

  • I bought a new laptop and used Windows Easy Transfer cable and moved all files from one computer to another. I installed iTunes and found my iTunes music Library.  However, when I plug in my iPod it says it is already synced with another iTunes Library.

    I bought a new laptop and used Windows Easy Transfer cable and moved all files from one computer to another. I installed iTunes and found my iTunes music Library.  However, when I plug in my iPod it says it is already synced with another iTunes Library. 
    I don't see anything in Help that shows when you already have transfered all the files over.  Why would it want to erase and sync when I already have all the music folder copied over?  I didn't have an issue when I had another technician copy from one laptop to another.  Home sharing is also on but not being recognized.

    I suspect you only migrated the media folder instead of the complete working library. Either review the transfer process and copy over the entire iTunes folder from your old profile's music folder or see Recovering your iTunes library from your iPod or iOS device.
    tt2

  • How to replace one variable with another in large number of queries

    Hello guys!
    I have this situation: our company needs to use one variable instead of another for certain amount of queries.
    Is there some way to automatically replace one variable with another for a certain characteristic in big number of queries ( like 150 - 200 ) ? Doing this manually would take lots of time)
    Apreciate your help!

    you could try (at your own risk) the following:
    1. search the technical id (ELTUID) of your variable in table RSZELTDIR
    2. see where this variable is used in table RSZELTXREF (by filling RSZELTDIR-ELTUIID in RSZELTXREF-TELTUID)
    3. add similar entries for your new variable
    4. delete the entries for the old variable (they're part of the key, so you can't simply "change" them)

  • How do i get all my data and info from my old iphone onto my new one without access to the original itunes that i set my first iphone up on? my partners itunes keeps telling me that its going to replace all my apps and data with his stuff

    how do i get all my data and info from my old iphone onto my new one without access to the original itunes that i set my first iphone up on? my partners itunes keeps telling me that its going to replace all my apps and data with his stuff

    ok so i have my own i tunes library - how do i get all my old apps onto my new library?

  • How can I replace ALL Contacts on the iPhone with my MobileMe Contacts?

    I have removed all duplicates in AddressBook on my various Macs and have uploaded the master copy to MobileMe. I would now like to replace all Contacts on my iPhone with that MobileMe master.
    I have followed the steps in the KB article for syncing the iPhone but when I get to the step of turning on the iPhone Settings for the Contacts it looks like the sync wants to merge the contacts. Of course, I do not want to "merge" since that will reintroduce all those duplicates.
    I suppose I could manually delete each of the the hundreds of contacts that are on my phone until it is empty ... and then turn on the iPhone setting for Contacts. That way it will have no option other than "add." But surely there is a more efficient way.
    (FWIW, when I turned OFF the setting for the Calendar it did empty the iPhone's calendar, which is what I wanted. But it didn't do that for the Contacts -- I turned that setting off but I was not asked if I wanted to keep or toss the Contacts. They are all still on the phone.)
    This is an OG iPhone, if that matters.

    Matthew Smith wrote:
    ... The only way I can think of doing it otherwise is possibly by syncing via iTunes via another Mac OS X user account where the address book is empty and then deleting them there and resyncing. ...
    Brilliant! That worked like a charm.
    I created a new User Account on one of my Macs and logged into it. I connected my iPhone to its iTunes and checked my iPhone's Sync Contacts option in iTunes. Since that account's address book was basically empty (two dummy contacts, one for Apple and one for this new user), I ended up with only those two Contacts on the phone. Then, going back to my real User account (which does not have syncing turned on in iTunes) the phone synced nicely with the MobileMe account and populated the phone with all the MobileMe contacts and groups.

  • When I merge one project with another, Aperture for some strange reason, randomly scrambles the photos from the project being merged, instead of keeping them in the order they were shot. What can I do to fix this?

    When I merge one project with another, Aperture 3 randomly scrambles the photos of the project being merged, instead of keeping them in the chronological order that I shot them in. What can I do to correct this?

    The photos have just been dumped into a new home and need to be told how to be arranged.  Go to the top left of the browser view for the merged project and select the desired sort order from the drop-down and they'll all sort according to that setting.

  • Universally replace one character with another and then change font

    Hello,
    I have a geologic map file where there are hundreds of labels like Cc, Cz, Cwu, etc... all in the Arial font. I need to change all of the capital C's with a character from the StratagemAge font. That is to say, I need to find all instances of one character in my file and then replace them with another character and then change the font of that selected character.
    I've been trying to figure out how to write an action to do this, but I was wondering if there is a way to do this easily already.
    Any suggestions?
    Jeremy

    > how do you turn off the dialog for the Find/Replace in action palette?
    At the left of each Action step is a square field. Mouse over it and the tooltip will read "Toggle Dialog On/Off".
    > I got an action that I wrote to work, but it takes a button click for each instance in my file that I want to change. Would yours have that same drawback as that?
    Yes, you would have to click each time to run the Action (or, you could assign a function key), but there would be no dialog to dismiss.
    But you can pile several iterations of the same steps in the Action; so that one execution of the Action does ten replacements with one click, for example.
    This would not be a particularly difficult thing to Javascript, but you are correct that scripting has a learning curve. This task is not something I would think often needed. Doing a few hundred replacements in a file, using an Action would not seem overly burdensome to me. But if it is a recurring need for you, it might be worth writing a script.
    JET

  • ABAP code help to replace one field with another

    Hi All,
    I have one DSO which is holding data for fields WBS(Old) and Company Code(Old).
    and I have one table which holds the information for Ols WBS and Ols Comp Code along with their New WBS and New Comp code.
    for eg:
    In DSO :
    Old WBS , Old Comp.Code
    abc        ,  001
    xyz        , 002
    In Table:
    Old WBS , Old Comp.Code , New WBS , New Comp.Code
    abc,           001,                    ABC,            001-04
    xyz,           002,                    XYZ,            002-04
    Now my requirement is ,
    search record by record from DSO and look into the table .
    If abc record in DSO matches with abc record in table, then in DSO it should replace with new WBS and Com.Codes.
    after the code, data in DSO shuld be lik this.
    WBS , Comp.Code
    ABC ,  001-04
    XYZ,   002-04.
    Thanks in advance,
    Sai Chand.S

    Hi
    If these fields are key fields dont think its possible to overwrite. Else plz write the following Code in End Routine it will overwrite the existing values in DSO.
    Select * from DSO into it_dso_temp. "U can even select only key fields WBS and CC fields.
    If sy_subrc=0.
    Sort it_dso_temp by WBS CC.
    endif.
    Select old WBS
               old CC
               new WBS
               new CC
    from Table
    into it_table
    for all entries in it_dso_temp
    where old WBS = WBS and old CC = CC.
    if sy_subrc=0.
    sort it_table by old WBS, old CC.
    endif.
    Loop at it_dso_temp assigning result_fields.
    Read it_table into wa_table with key old WBS = fs_dso_temp-WBS
                                                               old CC = fs_dso_temp-CC Binary SEarch.
    If sy_subrc = 0.
    result_fields-WBS = wa_table-new WBS.
    result_fields-CC = wa_table-new CC.
    endif.
    Endloop.
    Refresh result_package.
    result_package[] = it_dso_temp[].
    Also if incoming data has only old values u can loop at result package instead of it_dso_temp. let me know in case of issues.
    hope this helps !
    Reg
    Aparna

  • Is there an easy way to replace individual elements in one string array from another striing array?

    I have a string array with elements as follows:
       00 00 00 00 00 00 00 00
       00 00 00 00 00 00 00 00
       00 00 00 00 5E 00 00 00
       00 00 50 00 30 00 33 00
       34 00 39 00 00 00 00 00
       00 00 00 00 00 00 00 00
       00 00 00 00 00 00 00 00
    The array I want to use to replace values with is:
    43 4E 31 30 30 33 30 30 31 41
    Where 43 goes in the 3rd row fifth position
    Where 4E goes in the 3rd row seventh position
    Where 31 goes in the 4th row  first position
    Where 30 goes in the 4th row third position
    Where 30 goes in the 4th row fifth position
    Where 33 goes in the 4th row seventh position
    Where 30 goes in the 5th row  first position
    Where 30 goes in the 5th row third position
    Where 31 goes in the 5th row fifth position
    Where 41 goes in the 5th row seventh position
    So the final array looks as follows:
       00 00 00 00 00 00 00 00
       00 00 00 00 00 00 00 00
       00 00 00 00 43 00 4E 00
       31 00 30 00 30 00 33 00
       30 00 30 00 31 00 41 00
       00 00 00 00 00 00 00 00
       00 00 00 00 00 00 00 00
    Is there an easy way to replace an element of a string within an array with another element of a string within a different array?
    Thank you.
    Solved!
    Go to Solution.
    Attachments:
    CMM_Add_SCAN_SN_2_PAT_DATA (temp).vi ‏13 KB

    Try this
    Omar
    Message Edited by Omar II on 03-24-2010 03:25 PM
    Omar
    Attachments:
    CMM_Add_SCAN_SN_2_PAT_DATA%20(temp)[1].vi ‏17 KB

  • How do I move all files from one project, to another hard drive?

    I am using CS3- I created a project on one hard drive, began editing, etc.- but now I'd like to move that project to another hard drive.
    Where are all of the files associated with it so I can move it successfully?
    Thanks!

    You will also find links to many
    free tutorials in the PremiereProPedia that will quickly show you how things are done in Premiere Pro.
    Cheers
    Eddie
    PremiereProPedia   (
    RSS feed)
    - Over 300 frequently answered questions
    - Over 250 free tutorials
    - Maintained by editors like
    you
    Forum FAQ

  • Replacing colour/texture from one image onto another?

    Desperately need some guidance here as this has been driving me nuts....
    I want to use the colour and texture from one photo onto my own design. Now, using the rubber stamp tool I am able only to clone a certain area that moves with the cross hair, but how can I clone a section from one photo and use that as a fill to create another design? In other words, clone a colour from one photo as a colour fill for another design?
    Even using the colour replace tool and selecting source: image I am left with the colour pallette as the destination and cannot use the colour from the image to paint my new image from it.
    Thanks a heap

    If I want to copy a single color from one image to another, here's what I'll do:
    Create a new rectangle in the source image
    With the rectangle selected, use the eyedropper to pick a color from someplace in the source image
    Copy the rectangle and paste it in the destination image
    Select the object I want to color and use the eyedropper tool to select the color from the copied rectangle
    (A layer with rectangles of different colors is sometimes handy for setting up a color scheme and using it consistently.)
    You can also create bitmap patterns to use as fills for vector objects. Fireworks comes with quite a few, but it's easy to add your own. Here are a couple of tutorials
    http://www.entheosweb.com/fireworks/patterns.asp
    http://www.peachpit.com/articles/article.aspx?p=30942&seqNum=4

  • How to communicating one application with another?

    I wanted to make a web site that have the ability to view,update,insert and delete record in a database. How to do that? How can one application communicate with another? example, I wanted to have XHTML communicate with Java and Java to communicate with my database that is pointbase database.

    Multi-tier application communication is a somewhat complex matter. It may be made using differents techniques and designs. But like Sum-shusSue said, a typical Java-based solution would use these Java technologies: JSP (Java Server Page) for the presentation side (the client), Servlet or EJB (Enterprise Java Bean) for the server-side logic. The communication with the Database is made through JDBC.
    I suggest that you take a look at the J2EE edition of Java. That stands for Java 2 Enterprise Edition and it is specifically made for enterprise system development, such as Servlet, JSP, EJB, etc...
    This is a whole lot of new subjects and, from my humble experience, it is not that easy. i.e. I would not recommend it for strict newbies. However, if you have some months of Java coding on your side, maybe you should take a look. You can check first the left menu of this site, you'll find, under Technologies the sub-menu J2EE. There's also, somewhere on the site, tutorials on differents J2EE topics. Finally, I would recommend you this book:
    Professional Java Server Programming, J2EE 1.3 Edition, edited by Wrox. Of course, there a lot of other books on hte market. J2EE is a hot topic!
    Hope this help...
    Simon

  • How do you get all downloads from one computer to another

    How do I get everything on iTunes from one computer to another?

    You've posted to the iTunes Match forum, which your question isn't really related to. However this KB article will help:<http://support.apple.com/kb/HT4527>.

Maybe you are looking for