Results 1 to 20 of 23
- 07-02-2008, 07:06 AM #1
Member
- Join Date
- Jul 2008
- Location
- india
- Posts
- 35
- Rep Power
- 0
[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.
- 07-02-2008, 07:20 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 07-02-2008, 07:29 AM #3
Member
- Join Date
- Jul 2008
- Location
- india
- Posts
- 35
- Rep Power
- 0
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, 07:51 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Do it as much as in simple way.
You have formated the original string in wrong 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);
- 07-02-2008, 08:09 AM #5
Member
- Join Date
- Jul 2008
- Location
- india
- Posts
- 35
- Rep Power
- 0
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, 08:13 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 07-02-2008, 08:16 AM #7
Member
- Join Date
- Jul 2008
- Location
- india
- Posts
- 35
- Rep Power
- 0
yes!!!can u suggest sm solution??
- 07-02-2008, 08:21 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Solution is on the previous post. Did you read it?
- 07-02-2008, 08:24 AM #9
Member
- Join Date
- Jul 2008
- Location
- india
- Posts
- 35
- Rep Power
- 0
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, 08:33 AM #10
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Replace the "Speed" key with the existing one you wants to. Seems to me you have replaced a node with it's value.
- 07-02-2008, 08:43 AM #11
Member
- Join Date
- Jul 2008
- Location
- india
- Posts
- 35
- Rep Power
- 0
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, 08:48 AM #12
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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?
- 07-02-2008, 08:59 AM #13
Member
- Join Date
- Jun 2008
- Posts
- 43
- Rep Power
- 0
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, 09:29 AM #14
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
String replacements and everything is depend on him. Because I'm not clear what he says on the way of this thread.
- 07-02-2008, 10:55 AM #15
Member
- Join Date
- Jul 2008
- Location
- india
- Posts
- 35
- Rep Power
- 0
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, 01:37 PM #16
Member
- Join Date
- Jul 2008
- Location
- india
- Posts
- 35
- Rep Power
- 0
is there a method for that??
- 07-03-2008, 03:36 AM #17
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
As I said eralier Im bit confusing what you saying here.
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.Java Code:<description>Speed</description>
- 07-03-2008, 07:17 AM #18
Member
- Join Date
- Jun 2008
- Posts
- 4
- Rep Power
- 0
Is this the thing you need:
str=str.replace(">Speed<","XXXXX");
- 07-03-2008, 07:20 AM #19
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
No comments at all, from our thread starter. :)
- 07-08-2008, 07:20 AM #20
Member
- Join Date
- Jul 2008
- Location
- india
- Posts
- 35
- Rep Power
- 0
Similar Threads
-
string replace problem
By soni in forum Advanced JavaReplies: 8Last Post: 07-06-2008, 01:21 AM -
Reverse and Replace a String in Linear Time
By colin.cruise in forum New To JavaReplies: 5Last Post: 07-01-2008, 09:02 PM -
Finding the exact length of a string in a JLabel (in pixels)
By Clarkey in forum Advanced JavaReplies: 2Last Post: 03-25-2008, 05:49 AM -
Find and replace ( in a String
By hamish10101 in forum New To JavaReplies: 6Last Post: 01-17-2008, 05:51 AM -
String replace method
By venkata.tarigopula in forum Advanced JavaReplies: 1Last Post: 07-10-2007, 08:14 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks