gpt4 book ai didi

java - 修剪列表元素中的字符

转载 作者:行者123 更新时间:2023-12-01 13:32:09 24 4
gpt4 key购买 nike

我有以下代码:

    public class HtmlParser {

private Document doc;
private List<String>dataList;

public HtmlParser(Properties configuration) throws IOException {

String url = configuration.getProperty("url");
Document doc = Jsoup.connect(url).get();

this.dataList = new ArrayList<String>();
Elements tableRows = doc.getElementsByTag("tr");

for (Element tr : tableRows){
dataList.add(tr.text()+"\n");
}

System.out.println(this.dataList);

打印出以下内容:

[Flights on time 93%
, Within 1 hour 99%
, FLIGHT FROM TO DEPART ARRIVE STATUS
, FR 2111 Manchester Paphos 06:25 13:20 Landed 13:40
, FR 8582 London Stansted Thessaloniki 06:30 11:45 Landed
, FR 6093 Gran Canaria Milan Bergamo 06:35 12:05 Landed Early 11:47
, FR 4002 Lanzarote Milan Bergamo 07:15 12:25 Landed
, FR 2482 Leeds Bradford Riga 07:20 11:55 Landed
, FR 5212 Marrakesh Paris Beauvais 07:20 11:50 Landed Early 11:40
, FR 1041 Gran Canaria Stockholm Skavsta 07:45 14:30 On Time
, FR 6342 Rome Fiumicino Barcelona El Prat 08:15 10:00 Estimated Arrival 14:35
, OPERATING FROM FIUMICINO DUE TO WEATHER DIVERSION - PASSENGERS TO BE COACHED TO FIUMICINO AIRPORT
, FR 4172 Porto Frankfurt Hahn 08:15 11:45 Landed
, FR 4883 Rome Fiumicino Cagliari 08:20 09:30 Landed 12:50
, FR 7062 Dublin Alicante 08:30 12:25 Landed Early 12:05
, FR 1015 Tenerife Sth Stockholm Skavsta 08:30 15:15 On Time
, FR 7158 Dublin Madrid 08:45 12:35 Landed Early 12:15
, FR 8405 London Stansted Wroclaw 08:50 11:50 Landed
, FR 2065 Porto Dortmund 08:55 12:30 Landed
, FR 1031 Tenerife Sth Oslo Rygge 09:00 15:30 On Time
, FR 9283 Faro London Stansted 09:05 11:55 Landed Early 11:46
, FR 3071 London Stansted Comiso 09:05 13:15 Landed Early 13:10
, FR 3006 London Stansted Rome Ciampino 09:10 12:35 Landed
]

首先,我想删除 , 以及行开头的空格,并让文本以 FR 开头。我尝试过 dataList.remove(0) 但它不起作用。奇怪的是,在控制台显示中,所有行都在每个“,”之前用新行分隔,但它没有显示在此处。

我希望通过关键字(如斯坦斯特德)选择特定行,并在其中添加文本(如“到达时间:”等),但我在这里遇到了麻烦。

我对 Java 相当陌生,并且在我来到这里时正在学习。任何帮助将不胜感激。

最佳答案

帮助您学习的提示:

Firstly, I want to remove the , and the whitespace from the start of the rows and have the text starting with FR.

了解 Stringtrimsubstring 方法。事实上,我建议阅读整页......以及 List 的页面和 Collection ...

请记住,所有 String 对象都是不可变的,因此 trim 等都是通过创建字符串来工作的。因此,要使更新“粘住”,您需要将每个列表元素替换为更新后的值。

(另见下文!)

I've tried dataList.remove(0) but it doesn't work.

该方法从列表中删除一个条目。

Oddly enough, in the console display, all the rows are separated with a new line before each "," but it isn't showing up here.

事实上是/他们是。这些就是导致输出中每个逗号之前换行的原因......以及逗号本身!当您调用此方法时:

    System.out.println(this.dataList);

该列表正在由列表对象的 toString() 方法进行格式化。这就是添加方括号和神秘的 ", " 的东西。

在开始编写代码以从字符串中编辑这些神秘字符之前,请检查它们是否确实存在......而不仅仅是您打印列表的方式的产物。

I'm looking to select a particular row by keyword like Stanstead, and add text into the middle of it like "Arrival Time:" etc, but I'm having trouble here.

迭代列表,并对每个字符串调用contains。 javadoc 位于上面链接的位置...

关于java - 修剪列表元素中的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21511414/

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