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

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度