dongxie3701 2018-05-09 10:32
浏览 52

我从java中的php文件中读取代码,我希望按事件分割代码行

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();
  }
}

I have this code which reads from a php file and displays this output:

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

I would like to split each event and its information by its own in an array for example and then output each array in a jtextfield for example the first entry would be

Event Number: 1
Date: 2018-05-03
Time: 01:34:37
Event: meeting with friend

and so on

  • 写回答

1条回答 默认 最新

  • dsfdsf21321 2018-05-09 10:48
    关注

    This trick may help you

    Input

    $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';
    

    Solution

    Add a special element or word that not occur in the string before Event Number and removes the first occurrence of that by ltrim. The str_replace will replace Event Number to |Event Number.

    I used | here.

    Code is as follows:

    $string = ltrim(str_replace('Event Number','|Event Number',$string),'|');
    $array = explode('|',$string);//convert the string into array.
    echo "<pre>";print_r($array);
    

    Output

    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
    )
    
    评论

报告相同问题?

悬赏问题

  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝