这个话题已经在这里讨论了很多,但是没有一个解决方案对我有用。我想替换从 HTML 获得的字符串的一部分。获取和显示 HTML 工作正常,但我无法删除字符串的任何部分。它的行为就像没有找到它一样。
请看一下下面的代码:
public class Main extends Activity {
public static String URL = "";
public static String htmlString;
public TextView mainText;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
mainText = (TextView) findViewById(R.id.mainText);
constructURL();
getHtml();
htmlString.replaceAll("<head>", "hulo");
mainText.setText(htmlString);
}
public void getHtml() {
try {
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpGet httpGet = new HttpGet(URL);
HttpResponse response = httpClient.execute(httpGet, localContext);
BufferedReader reader = new BufferedReader(
new InputStreamReader(
response.getEntity().getContent()
)
);
String line = null;
while ((line = reader.readLine()) != null){
htmlString += line + "\n";
}
} catch (Exception e) {
}
}
public void constructURL() {
Time time = new Time();
time.setToNow();
String year = convertToString(time.year - 2000);
String month = convertToString(time.month + 1);
String monthDay = convertToString(time.monthDay);
URL = "http://www.gymzl.cz/bakalari/suplovani_st/tr" + year + month + monthDay + ".htm";
}
public String convertToString(int value) {
String text = "";
if(value < 10) text = "0";
text += String.valueOf(value);
return text;
}
}
“hulo”替代品似乎不起作用。
很抱歉代码这么长,但我已经尝试了一切。
最佳答案
replaceAll
不会更新调用字符串,您需要将其分配回来。改变这个
htmlString.replaceAll("<head>", "hulo");
至
htmlString = htmlString.replaceAll("<head>", "hulo");
关于java - 更换琴弦部件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23057561/