2016-11-13 50 views
1

我在HTML获得web的字符串值。存储在变量s中,如下所示。在JAVA中更改HTML值(Android)

<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");但它不工作。

任何线索......我该如何改变它。

+0

你有没有躲过双引号?就像这个“\”“ –

+0

实际发生了什么?执行前一行后s的值是多少? –

回答

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); 
+0

你是正确的感觉转储......非常感谢。 –