android 字符串换行符,\ n(换行符)删除Android
調用Html.fromHtml(String)會將
正確翻譯為\ n。
String html = "06:00 Vesti
07:15 Something Else
09:10 Movie ...
15:45 Something..";
String str = Html.fromHtml(html).toString();
String[] arr = str.split("\n");然后,只需將它拆分為行 - 不需要regexp(在第一種情況下你不應該使用它來解析HTML)。
編輯 b>:將所有內容變成一堆Dates
// Used to find the HH:mm, in case the input is wonky
Pattern p = Pattern.compile("([0-2][0-9]:[0-5][0-9])");
SimpleDateFormat fmt = new SimpleDateFormat("HH:mm");
SortedMap programs = new TreeMap();
for (String row : arr) {
Matcher m = p.matcher(row);
if (m.find()) {
// We found a time in this row
ParsePosition pp = new ParsePosition(m.start(0));
Date when = fmt.parse(row, pp);
String title = row.substring(pp.getIndex()).trim();
programs.put(when, title);
}
}
// Now programs contain the sorted list of programs. Unfortunately, since
// SimpleDateFormat is stupid, they're all placed back in 1970 :-D.
// This would give you an ordered printout of all programs *AFTER* 08:00
Date filter = fmt.parse("08:00");
SortedMap after0800 = programs.tailMap(filter);
// Since this is a SortedMap, after0800.values() will return the program names in order.
// You can also iterate over each entry like so:
for (Map.Entry program : after0800.entrySet()) {
// You can use the SimpleDateFormat to pretty-print the HH:mm again.
System.out.println("When:" + fmt.format(program.getKey()));
System.out.println("Title:" + program.getValue());
}
總結
以上是生活随笔為你收集整理的android 字符串换行符,\ n(换行符)删除Android的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JAVA 局域网聊天_JAVA实现局域
- 下一篇: 前程无忧:节后芯片、通讯行业人才需求看涨