Reputation: 491
I am try to replace below String
#cit {font-size:16pt; color:navy; text-align:center; font-weight:bold;}
With
#cit {font-size:16pt; color:red; text-align:center; font-weight:bold;}
i am writting a Java code for this
strbuf.toString().replaceAll(Pattern.quote("#cit {font-size:16pt; color:navy;
text-align:center; font-weight:bold;}"), "#cit {font-size:16pt; color:red;
text-align:center; font-weight:bold;}");
but the String not get replace? please help me
Upvotes: 0
Views: 249
Reputation: 1427
You just want to change the color?
use:
strbuf.toString().replaceAll("navy", "red");
Btw.: If you really want to replace values in CSS it would be better to insert some kind of markes into the source CSS like color:${color-to-be-replaced} and replace that markers because it is more portable and reliable. Think about what will happen if you change the color of source CSS to green and forget to change the replacement code.
Upvotes: 0
Reputation: 2040
I Guess you are using a StringBuffer.
strbuf = new StringBuffer(strbuf.toString().replace(
"#cit \\{font-size:16pt; color:navy; text-align:center; font-weight:bold;\\}"),
"#cit \\{font-size:16pt; color:red; text-align:center; font-weight:bold;\\}"));
Because:
toString()
will create a copy of the StringBuffer
. If you replace text in the copy, this will not change strbuf
!
Whereat \\
is used to mask { and } as an not-regexp.
Upvotes: 1
Reputation: 10997
You cant do string replace by doing this.
mystring.replace("string1", "string2");
Instead you should do
mytring = mystring.replace("string1", "string2");
This is because String is immutable object in java. str.replace wont affect the original str object but instead method returns a new string object.
Upvotes: 0
Reputation: 15890
I would use String.replace() to avoid having to deal with regexes in replaceAll()
String result = text.replace("#cit {font-size:16pt; color:navy; text-align:center; font-weight:bold;}",
"#cit {font-size:16pt; color:red; text-align:center; font-weight:bold;}");
Upvotes: 0
Reputation: 35597
What about
str.replaceAll(str,"#cit {font-size:16pt; color:red; text-align:center; font-weight:bold;}")
Upvotes: 0
Reputation: 1777
change
strbuf.toString().replaceAll(Pattern.quote("#cit {font-size:16pt; color:navy; text-align:center; font-weight:bold;}"), "#cit {font-size:16pt; color:red; text-align:center; font-weight:bold;}");
to
strbuf.toString().replaceAll("#cit {font-size:16pt; color:navy; text-align:center; font-weight:bold;}", "#cit {font-size:16pt; color:red; text-align:center; font-weight:bold;}");
Upvotes: 3