Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-02-2008, 08:06 AM
Member
 
Join Date: Jul 2008
Location: india
Posts: 22
pankaj_salwan is on a distinguished road
[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 04:27 PM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-02-2008, 08:20 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,486
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-02-2008, 08:29 AM
Member
 
Join Date: Jul 2008
Location: india
Posts: 22
pankaj_salwan is on a distinguished road
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??
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 07-02-2008, 08:51 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,486
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Do it as much as in simple way.

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.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 07-02-2008, 09:09 AM
Member
 
Join Date: Jul 2008
Location: india
Posts: 22
pankaj_salwan is on a distinguished road
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...??
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 07-02-2008, 09:13 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,486
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 07-02-2008, 09:16 AM
Member
 
Join Date: Jul 2008
Location: india
Posts: 22
pankaj_salwan is on a distinguished road
yes!!!can u suggest sm solution??
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 07-02-2008, 09:21 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,486
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Solution is on the previous post. Did you read it?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 07-02-2008, 09:24 AM
Member
 
Join Date: Jul 2008
Location: india
Posts: 22
pankaj_salwan is on a distinguished road
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..
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 07-02-2008, 09:33 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,486
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Replace the "Speed" key with the existing one you wants to. Seems to me you have replaced a node with it's value.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 07-02-2008, 09:43 AM
Member
 
Join Date: Jul 2008
Location: india
Posts: 22
pankaj_salwan is on a distinguished road
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???
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 07-02-2008, 09:48 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,486
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 07-02-2008, 09:59 AM
pao pao is offline
Member
 
Join Date: Jun 2008
Posts: 41
pao is on a distinguished road
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?
Bookmark Post in Technorati
Reply With Quote
  #14 (permalink)  
Old 07-02-2008, 10:29 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,486
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
String replacements and everything is depend on him. Because I'm not clear what he says on the way of this thread.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #15 (permalink)  
Old 07-02-2008, 11:55 AM
Member
 
Join Date: Jul 2008
Location: india
Posts: 22
pankaj_salwan is on a distinguished road
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????????????
Bookmark Post in Technorati
Reply With Quote
  #16 (permalink)  
Old 07-02-2008, 02:37 PM
Member
 
Join Date: Jul 2008
Location: india
Posts: 22
pankaj_salwan is on a distinguished road
is there a method for that??
Bookmark Post in Technorati
Reply With Quote
  #17 (permalink)  
Old 07-03-2008, 04:36 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,486
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
As I said eralier Im bit confusing what you saying here.

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.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #18 (permalink)  
Old 07-03-2008, 08:17 AM
Member
 
Join Date: Jun 2008
Posts: 3
Hassan Hafez is on a distinguished road
Is this the thing you need:
str=str.replace(">Speed<","XXXXX");
Bookmark Post in Technorati
Reply With Quote
  #19 (permalink)  
Old 07-03-2008, 08:20 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,486
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
No comments at all, from our thread starter.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #20 (permalink)  
Old 07-08-2008, 08:20 AM
Member
 
Join Date: Jul 2008
Location: india
Posts: 22
pankaj_salwan is on a distinguished road
Thanks!!!!eranga...
problem solved....instead of using replaceAll...i used setTextContext and updated The XMl file itself...
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On