有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java我想更新字符串数据,其中包含双引号和分号

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 class="text__white">Techno power</span></p>";
  }
}

用户输入上面的字符串

我正在尝试将文本\uuuu白色更新为文本\uuuu红色

但在任何字符串对象中输入上述字符串都是一个弹出错误


共 (2) 个答案

  1. # 1 楼答案

    可以在每个特殊字符前面使用前斜杠 如下-

    String str="<p style=\"text-align: center;\"><span class=\"text__white\">Techno power</span></p>";
    
  2. # 2 楼答案

    我知道上面的答案更正了字符串,但下面是如何使用String.replace(String str1, String str2)方法将单词text__white替换为text__red

    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.replace("text__white", "text__red");
        System.out.println(str);
        System.out.println(strrep);
      }
    }
    

    您可以从这里在代码中实现所需的逻辑