Page 1 of 2 12 LastLast
Results 1 to 20 of 23
  1. #1
    pankaj_salwan is offline Member
    Join Date
    Jul 2008
    Location
    india
    Posts
    35
    Rep Power
    0

    Default [SOLVED] how to replace exact string in java

    hi!!
    string str=
    <schemaVersion>6.2.0</schemaVersion>
    <version>
    <label>6.2.0.0.16</label>
    <majorVersion>1.0</majorVersion>
    </version>
    <schedule name="no_schedule">
    <description>Speed</description>
    </schedule>
    <region name="no_region">
    <description>Speed is done</description>
    </region>
    </model>
    using replaceall() in java
    i parsed this string on description tag and taken the speed out..i want to replace speed with "hiz"..bt is replacing "speed is done" with "hiz is done" also

    the string is big one and there are lot of "speed" word in it..plz tel me hw should it to be done...
    Last edited by pankaj_salwan; 07-09-2008 at 03:27 PM.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Is this from a xml file. So why don't you working with xml file it self. Basically you can find word "speed" in two locations. It can be caused when you search.

  3. #3
    pankaj_salwan is offline Member
    Join Date
    Jul 2008
    Location
    india
    Posts
    35
    Rep Power
    0

    Default

    actually xml file i shown is quite big one..and speed word is lot of times as a substring ..whereever there is speed word
    like speed is done,,speed over it is replacing that ..i want only speed to be replaced...can i do it with xml parsing only if yes then how??

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Do it as much as in simple way.

    Java Code:
            String str=
                "<schemaVersion>6.2.0</schemaVersion>"+
                "<version>"+
                "<label>6.2.0.0.16</label>"+
                "<majorVersion>1.0</majorVersion>"+
                "</version>"+
                "<schedule name=\"no_schedule\">"+
                "<description>Speed</description>"+
                "</schedule>"+
                "<region name=\"no_region\">"+
                "<description>Speed is done</description>"+
                "</region>"+
                "</model>";
            
            String s = str.replace("Speed", "XXXX");
            System.out.println(s);
    You have formated the original string in wrong way.

  5. #5
    pankaj_salwan is offline Member
    Join Date
    Jul 2008
    Location
    india
    Posts
    35
    Rep Power
    0

    Default

    actually i m reading a file in a string and operation on it..see
    the file is xml file and i parsed it using DOM and taken the value in description field ..i have a property file i want words in the property file to be changed with corresponding words in that string bt problem is in my code.. whenever it replaced speed with "his" it also replaced other occurences like "Speed is done" ...i want only speed to be changed...

    i think now u got the problem...??

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Ok, you can read the xml file and get the value of description field right? So that value do the replace process and write/update xml file with the new value.

  7. #7
    pankaj_salwan is offline Member
    Join Date
    Jul 2008
    Location
    india
    Posts
    35
    Rep Power
    0

    Default

    yes!!!can u suggest sm solution??

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  9. #9
    pankaj_salwan is offline Member
    Join Date
    Jul 2008
    Location
    india
    Posts
    35
    Rep Power
    0

    Default

    i am doing that
    if(prop.containsKey(Node_val_sp))
    {
    str1=prop.getProperty(Node_val_sp);
    System.out.println("FOUND MATCH KEY::"+ Node_val_sp);
    System.out.println("FOUND MATCH VALUE::"+ str1);
    //requestStr=requestStr.replaceAll(Node_val,Node_val _sp);
    requestStr=requestStr.replaceAll(Node_val_sp,str1) ;
    }

    bt not working..

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Replace the "Speed" key with the existing one you wants to. Seems to me you have replaced a node with it's value.

  11. #11
    pankaj_salwan is offline Member
    Join Date
    Jul 2008
    Location
    india
    Posts
    35
    Rep Power
    0

    Default

    i am doing that...
    the problem is with replacement... just tell me can i replace a actual string to be replaced ..actually i don't want sbstring like speed in "isitspeed" to be replaced as isithis..just it must replace speed nothing else....is there any function or i have to code myself???

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    What you asking is not bit clear to me now.

    First, you can read the file and value(actually a string), right?

    Then what you want to do is, add something to the string or replace some part to it?

  13. #13
    pao
    pao is offline Member
    Join Date
    Jun 2008
    Posts
    43
    Rep Power
    0

    Default

    I would use JDOM or something similar, and update the text value of schedule\description if this is the only speed you are interested.

    Why are you using string replace methods if you have an xml file?

    Surely the reason you created the xml schema was to describe some data and make it easier to work with than a string?

  14. #14
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    String replacements and everything is depend on him. Because I'm not clear what he says on the way of this thread.

  15. #15
    pankaj_salwan is offline Member
    Join Date
    Jul 2008
    Location
    india
    Posts
    35
    Rep Power
    0

    Smile replace string and not the substrings

    u plz tel me the method(inbuild or user defined) to replace only the exact string and not the substrings????????????

  16. #16
    pankaj_salwan is offline Member
    Join Date
    Jul 2008
    Location
    india
    Posts
    35
    Rep Power
    0

    Default

    is there a method for that??

  17. #17
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    As I said eralier Im bit confusing what you saying here.

    Java Code:
    <description>Speed</description>
    You want to replace the string "Speed" with what you want, right? You said that you can read that string too. I told you how to replace that string. After replacing write that value to the xml file again. That's it, I don't know where you stuck with.

  18. #18
    Hassan Hafez is offline Member
    Join Date
    Jun 2008
    Posts
    4
    Rep Power
    0

    Default

    Is this the thing you need:
    str=str.replace(">Speed<","XXXXX");

  19. #19
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  20. #20
    pankaj_salwan is offline Member
    Join Date
    Jul 2008
    Location
    india
    Posts
    35
    Rep Power
    0

    Default

    Thanks!!!!eranga...
    problem solved....instead of using replaceAll...i used setTextContext and updated The XMl file itself...

Page 1 of 2 12 LastLast

Similar Threads

  1. string replace problem
    By soni in forum Advanced Java
    Replies: 8
    Last Post: 07-06-2008, 01:21 AM
  2. Reverse and Replace a String in Linear Time
    By colin.cruise in forum New To Java
    Replies: 5
    Last Post: 07-01-2008, 09:02 PM
  3. Replies: 2
    Last Post: 03-25-2008, 05:49 AM
  4. Find and replace ( in a String
    By hamish10101 in forum New To Java
    Replies: 6
    Last Post: 01-17-2008, 05:51 AM
  5. String replace method
    By venkata.tarigopula in forum Advanced Java
    Replies: 1
    Last Post: 07-10-2007, 08:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •