gpt4 book ai didi

android -\n(换行符)删除 Android

转载 作者:行者123 更新时间:2023-11-30 04:04:21 26 4
gpt4 key购买 nike

有一些 XML 解析文本看起来像这样:

06:00 Vesti<br>07:15 Something Else<br>09:10 Movie<a href="..."> ... <br>15:45 Something..

而且有很多..

好吧,我已经做到了:

String mim =ses.replaceAll("(?s)\\<.*?\\>", " \n");

没有其他方法可以很好地显示文本。现在,经过几次放映和一段时间后,我需要将相同的文本分成单独的字符串,如下所示:

06:00 Vesti   

... 或

07:15 Something Else

我已经尝试过类似的方法,但它不起作用:

char[] rast = description.toCharArray();
int brojac = 0;
for(int q=0; q<description.length(); q++){
if(rast[q]=='\\' && rast[q+1]=='n' ) brojac++;
}
String[] niz = new String[brojac];

int bf1=0;
int bf2=0;
int bf3=0;
int oo=0;

for(int q=0; q<description.length(); q++){
if(rast[q]=='\\'&& rast[q+1]=='n'){
bf3=bf1;
bf1=q;

String lol = description.substring(bf3, bf1);
niz[oo]=lol;
oo++;
}
}

我知道在 description.substring(bf3,bf1) 中没有按应有的方式设置,但我认为:

if(rast[q]=='\\' && rast[q+1]=='n) 

那样不行..还有其他解决方案吗?

注意。没有其他方法可以获取该资源。 , 一定是通过这个。

最佳答案

调用 Html.fromHtml(String)将正确翻译 <br>进入\n。

String html = "06:00 Vesti<br>07:15 Something Else<br>09:10 Movie<a href=\"...\"> ... <br>15:45 Something..";
String str = Html.fromHtml(html).toString();
String[] arr = str.split("\n");

然后,只需按行拆分它 - 不需要正则表达式(在第一种情况下你不应该使用它来解析 HTML)。

编辑:将所有内容变成一堆 Date

// 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<Date, String> programs = new TreeMap<Date, String>();
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<Date, String> 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<Date,String> 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,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11987996/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com