import java.net.*;
import java.io.*;
import java.util.regex.Pattern;
public class AppDev {
public static void main(String[] args) throws Exception {
URL website = new URL("MyWebsite.php");
BufferedReader in = new BufferedReader(
new InputStreamReader(website.openStream()));
String inputLine;
StringBuilder sb = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
System.out.println(inputLine);
sb.append(inputLine);
}
in.close();
}
}
我有这个代码从php文件读取并显示此输出:
Event Number: 1
Date: 2018-05-16
Time: 12:00:00
Event: meeting with friend
Event Number: 5
Date: 2018-05-03
Time: 01:34:37
Event: what the hell
Event Number: 6
Date: 2018-05-03
Time: 01:35:05
Event: Rage at MySQL
Event Number: 16
Date: 2018-06-20
Time: 08:00:00
Event: hfdhfdfgfg
我想将每个事件及其信息在数组中自行拆分,然后在jtextfield中输出每个数组,例如第一个条目将是
Event Number: 1
Date: 2018-05-03
Time: 01:34:37
Event: meeting with friend
等等
答案 0 :(得分:0)
这个技巧可以帮到你
<强>输入强>
$string = 'Event Number: 1Date: 2018-05-16Time: 12:00:00Event: meeting with friendEvent Number: 5Date: 2018-05-03Time: 01:34:37Event: what the hellEvent Number: 6Date: 2018-05-03Time: 01:35:05Event: Rage at MySQLEvent Number: 16Date: 2018-06-20Time: 08:00:00Event: hfdhfdfgfg';
<强>解决方案强>
在事件编号之前添加字符串中未出现的特殊元素或单词,并按ltrim删除第一次出现的元素或单词。 str_replace会将Event Number
替换为|Event Number
。
我在这里使用|
。
代码如下:
$string = ltrim(str_replace('Event Number','|Event Number',$string),'|');
$array = explode('|',$string);//convert the string into array.
echo "<pre>";print_r($array);
<强>输出强>
Array
(
[0] => Event Number: 1Date: 2018-05-16Time: 12:00:00Event: meeting with friend
[1] => Event Number: 5Date: 2018-05-03Time: 01:34:37Event: what the hell
[2] => Event Number: 6Date: 2018-05-03Time: 01:35:05Event: Rage at MySQL
[3] => Event Number: 16Date: 2018-06-20Time: 08:00:00Event: hfdhfdfgfg
)