有 Java 编程相关的问题?

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

在JAVA(Android)中更改HTML值

我在HTMLget web中有字符串值。存储在变量s中,如下所示

<iframe width="560" height="315" src="https://www.youtube.com/embed/ubR8WfwQTkw" frameborder="0" allowfullscreen></iframe>

enter image description here

问题

我想将宽度值560更改为250

我尝试使用s.replace("560", "250");,但它不起作用

任何线索。。。我怎样才能改变它


共 (1) 个答案

  1. # 1 楼答案

    此代码适用于:

    String s = "<iframe width=\"560\" height=\"315\" src=\"https://www.youtube.com/embed/ubR8WfwQTkw\" frameborder=\"0\" allowfullscreen></iframe>";
    String replace = s.replace("560", "250");
    System.out.println(replace);
    

    如果打印s,您将看到560,因为字符串在java中是不可变的。如果不想声明新字符串,可以通过以下方式执行:

    String s = "<iframe width=\"560\" height=\"315\" src=\"https://www.youtube.com/embed/ubR8WfwQTkw\" frameborder=\"0\" allowfullscreen></iframe>";
    s = s.replace("560", "250");
    System.out.println(s);