Home > Software design >  I want to update the string data which has double quotes and semicolon in it
I want to update the string data which has double quotes and semicolon in it

Time:10-28

import java.util.*;

class ChangeCSS{

  public static void main(String[] args){
    System.out.println("Enter the Code to be converted to red css");
    Scanner sc=new Scanner("System.in");
    String str=sc.nextLine();

    //Entered by user String str="<p style="text-align: center;">. <span >Techno power</span></p>";
  }
}

User inputs the above string

I am trying to update text__white to text__red

But inputing the above string in any String object is popping error

CodePudding user response:

You can use front slash in front of every special char like below-

String str="<p style=\"text-align: center;\"><span class=\"text__white\">Techno power</span></p>";

CodePudding user response:

I know the above answer corrects your String, but here's how you can replace the word text__white with text__red using the String.replaceAll(String str1, String str2) method:

public class ReplaceString {
  public static void main(String[] args){
    String str = "<p style=\"text-align: center;\"><span class=\"text__white\">Techno power</span></p>";
    String strrep = str.replaceAll("text__white", "text__red");
    System.out.println(str);
    System.out.println(strrep);
  }
}

You can implement the required logic in your code from here.

  • Related