0
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

1
  • Are you sure that you can modify a toString() directly? Commented Sep 18, 2013 at 6:44

7 Answers 7

3

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;}");
Sign up to request clarification or add additional context in comments.

Comments

1

Just do:

myString = myString.replaceAll("navy", "red");

Comments

1

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.

3 Comments

Take care that strbuf is not final.
+1, actually the most decent answer here IMO given that we don't know how specifically the OP wants to replace "navy" by "red". Also, to everyone that suggests to use replace() / replaceAll() without saving the result into a variable: this won't work unless you only want to loose CPU time.
I am unhappy because its thread-unsafe... see, other threads may use the strbuf. If i just redeclare the variable it may cause yielding into two objects whereat there only should be one.
0

What about

 str.replaceAll(str,"#cit {font-size:16pt; color:red; text-align:center; font-weight:bold;}")

Comments

0

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;}");

Comments

0

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.