14 Replies Latest reply: Apr 22, 2009 10:51 AM by 800282 RSS

    How to do replace operations on string.

    843789
      Hi,
      I have a URl as following:

      url = jdbc:oracle:thin:@(description=(address_list=(ENABLE = broken)(LOAD_BALANCE=yes)(ADDRESS = (PROTOCOL = TCP)(HOST = myhost)(PORT = 1521))(address=(protocol=tcp)(port=1521)(myhost2)))(connect_data=(SERVICE_NAME=mysid)(failover_mode=(type=select)(method=basic)(retries=60)(delay=5))))

      I need to replace
      jdbc:oracle:thin:@ to oracle:

      therefore final string should be:

      url = oracle:(description=(address_list=(ENABLE = broken)(LOAD_BALANCE=yes)(ADDRESS = (PROTOCOL = TCP)(HOST = myhost)(PORT = 1521))(address=(protocol=tcp)(port=1521)(myhost2)))(connect_data=(SERVICE_NAME=mysid)(failover_mode=(type=select)(method=basic)(retries=60)(delay=5))))


      The following didnt work:
      str = str.replaceAll("jdbc:oracle:thin@", "oracle:");


      How can that be achieved?

      Please help with this
        • 1. Re: How to do replace operations on string.
          843789
          Look it up in the javadocs

          http://java.sun.com/javase/6/docs/api/
          • 2. Re: How to do replace operations on string.
            800282
            neeto wrote:
            ...
            The following didnt work:
            str = str.replaceAll("jdbc:oracle:thin@", "oracle:");
            It does with me.

            B.t.w.m you can also use replace(...) instead of replaceAll(...).
            • 3. Re: How to do replace operations on string.
              800308
              neeto,

              Are you planning to ask how to do every simple thing on the forums? That mightn't be the best approach.

              http://java.sun.com/docs/books/tutorial/

              I suggest you start at the bit where it says "Start Here".

              Cheers. Keith.
              • 4. Re: How to do replace operations on string.
                843789
                This didnt work for me

                public class ReplaceDemo {
                public static void main(String[] args) {
                String str = "jdbc:oracle:thin:@(description=(address_list=(ENABLE = broken)(LOAD_BALANCE=yes)(ADDRESS = (PROTOCOL = TCP)(HOST = myhost)(PORT = 1521))(address=(protocol=tcp)(port=1521)(host=myhost2)))(connect_data=(SERVICE_NAME=sid)(failover_mode=(type=select)(method=basic)(retries=60)(delay=5))))";
                str = str.replaceAll("jdbc:oracle:thin@:", "oracle:");
                System.out.println(str);
                }
                }
                • 5. Re: How to do replace operations on string.
                  843789
                  @ Keith
                  I think that the forum is named as new to java
                  • 6. Re: How to do replace operations on string.
                    800308
                    1. Use &#123code} tags.

                    2. Define "doesn't work"! (though in this case I can make an educated guess, we shouldn't have to guess).
                    • 7. Re: How to do replace operations on string.
                      843789
                      Doesnt work means

                      the output is :

                      jdbc:oracle:thin:@(description=(address_list=(ENABLE = broken)(LOAD_BALANCE=yes)
                      (ADDRESS = (PROTOCOL = TCP)(HOST =myhost)(PORT = 1521))(address
                      =(protocol=tcp)(port=1521)(host=myhost2)))(connect_data=(SERVICE_
                      NAME=mysid)(failover_mode=(type=select)(method=basic)(retries=60)(delay=5))))
                      Press any key to continue . . .
                      • 8. Re: How to do replace operations on string.
                        800282
                        neeto wrote:
                        This didnt work for me
                        ...
                        Now you're posting something different.
                        Look:
                        str = str.replaceAll("jdbc:oracle:thin@:", "oracle:"); // this is what you now posted
                        str = str.replaceAll("jdbc:oracle:thin@", "oracle:");  // this is what you posted before
                        Spot the difference.
                        • 9. Re: How to do replace operations on string.
                          800308
                          neeto wrote:
                          @ Keith
                          I think that the forum is named as new to java
                          Yes. And "New To Java" doesn't mean "Please be as useless as possible, and don't bother read anything... just ask the forumites every single question between hello world and MMRPG." You still need to read the manual you know.

                          But I see you've finally, nearly, asked a reasonable question... and I can also tell by your snipishness that you're either very young, and/or very stupid.

                          Cheers. Keith.
                          • 10. Re: How to do replace operations on string.
                            843789
                            whats with the attitude.. I dont think that you own this forum.

                            By the way please ignore the post if you think it is the waste of time..
                            And ya I found the solution, culprit was @

                            str = str.replaceAll("jdbc:oracle:thin:\\@", "oracle:");
                            • 11. Re: How to do replace operations on string.
                              800308
                              neeto wrote:
                              whats with the attitude.. I dont think that you own this forum.

                              By the way please ignore the post if you think it is the waste of time..
                              And ya I found the solution, culprit was @

                              str = str.replaceAll("jdbc:oracle:thin:\\@", "oracle:");
                              \\@ Good.

                              And no, I certainly do NOT own this forum. I just (increasing) get annoyed with little punks who want to treat these boards as a "do my homework for because I'm to lazy/pathetic" service.

                              We (the regulars) come here to help people with problems... interesting problems... mainly for the challenge... you not being able to read the manual isn't an interesting challenge.

                              You do know about the API doco, don't you? It over here: http://java.sun.com/javase/6/docs/api/

                              I'm sorry, it just grates more the 5,000th time. It's not you (or just you) that I'm reacting to... Over-sensitive.

                              Cheers. Keith.
                              • 12. Re: How to do replace operations on string.
                                PhHein
                                corlettk wrote:
                                I'm sorry, it just grates more the 5,000th time. It's not you (or just you) that I'm reacting to... Over-sensitive.
                                Well done, neeto, now he'll turn green again and wreak havoc in here.
                                • 13. Re: How to do replace operations on string.
                                  843789
                                  ya ya sure.. and I thought Oracle owns Sun!!
                                  • 14. Re: How to do replace operations on string.
                                    800282
                                    neeto wrote:
                                    ...
                                    And ya I found the solution, culprit was @

                                    str = str.replaceAll("jdbc:oracle:thin:\\@", "oracle:");
                                    No, that was not it. You had a colon after your @ sign when it didn't work:
                                    str = str.replaceAll("jdbc:oracle:thin:@:", "oracle:"); // See the colon after your @?
                                    str = str.replaceAll("jdbc:oracle:thin:@", "oracle:"); // this works
                                    And yes, escaping the @ with a backslash works, but isn't needed.