|
|
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.
|
|

07-02-2008, 08:06 AM
|
|
Member
|
|
Join Date: Jul 2008
Location: india
Posts: 22
|
|
|
[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.
|
|

07-02-2008, 08:20 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,486
|
|
|
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.
|
|

07-02-2008, 08:29 AM
|
|
Member
|
|
Join Date: Jul 2008
Location: india
Posts: 22
|
|
|
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??
|
|

07-02-2008, 08:51 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,486
|
|
Do it as much as in simple way.
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.
|
|

07-02-2008, 09:09 AM
|
|
Member
|
|
Join Date: Jul 2008
Location: india
Posts: 22
|
|
|
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...??
|
|

07-02-2008, 09:13 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,486
|
|
|
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.
|
|

07-02-2008, 09:16 AM
|
|
Member
|
|
Join Date: Jul 2008
Location: india
Posts: 22
|
|
|
yes!!!can u suggest sm solution??
|
|

07-02-2008, 09:21 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,486
|
|
|
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.
|
|

07-02-2008, 09:24 AM
|
|
Member
|
|
Join Date: Jul 2008
Location: india
Posts: 22
|
|
|
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..
|
|

07-02-2008, 09:33 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,486
|
|
|
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.
|
|

07-02-2008, 09:43 AM
|
|
Member
|
|
Join Date: Jul 2008
Location: india
Posts: 22
|
|
|
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???
|
|

07-02-2008, 09:48 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,486
|
|
|
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.
|
|

07-02-2008, 09:59 AM
|
|
Member
|
|
Join Date: Jun 2008
Posts: 41
|
|
|
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?
|
|

07-02-2008, 10:29 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,486
|
|
|
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.
|
|

07-02-2008, 11:55 AM
|
|
Member
|
|
Join Date: Jul 2008
Location: india
Posts: 22
|
|
|
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????????????
|
|

07-02-2008, 02:37 PM
|
|
Member
|
|
Join Date: Jul 2008
Location: india
Posts: 22
|
|
|
is there a method for that??
|
|

07-03-2008, 04:36 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,486
|
|
As I said eralier Im bit confusing what you saying here.
<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.
|
|

07-03-2008, 08:17 AM
|
|
Member
|
|
Join Date: Jun 2008
Posts: 3
|
|
|
Is this the thing you need:
str=str.replace(">Speed<","XXXXX");
|
|

07-03-2008, 08:20 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,486
|
|
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.
|
|

07-08-2008, 08:20 AM
|
|
Member
|
|
Join Date: Jul 2008
Location: india
Posts: 22
|
|
|
Thanks!!!!eranga...
problem solved....instead of using replaceAll...i used setTextContext and updated The XMl file itself...
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
| |