0

I'm writing a program where there is a particular string in a line to be replaced

Below is my sample program.

public class MyFirstJavaProgram {

    public static void main(String []args) {
        String x="<link rel=\"stylesheet\" type=\"text/css\" href=\"abc.css\" />";
        System.out.println(x);

    }
} 

In the above program I want to replace abc.css with xyz.css. I'm aware of general String replacement function like String.replace(oldString, newString), But here the problem is that abc.css changes from file to file. I want to replace anything.css with xyz.css.

Here is a working Example Fiddle

please let me know how can I do this.

Thanks

2
  • Don't bind your code to a hard coded string then... use regex Commented Mar 29, 2016 at 14:21
  • Hi @Xoce웃Пepeúpa, Any suggestion on how do i do it? Commented Mar 29, 2016 at 14:48

1 Answer 1

1
 x=x.replaceAll("(.*)\"(.*)\\.css", "$1\"xyz\\.css");
Sign up to request clarification or add additional context in comments.

4 Comments

This is not working as Expected. The output that I get is <link rel="stylesheet" type="textxyz.css" href="abcxyz.css" /> instead of <link rel="stylesheet" type="textxyz.css" href="xyz.css" />
Here is a live demo ideone.com/xw8e0r
Sorry, failed with the escape characters. I edit the answer and give you a live demo. I've taken in consideration that you must have it inside quotes ("...") ideone.com/WfWtHr
Thanks mate. This worked :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.