【问题标题】:How to split one line of text to multiple lines based on keyword/pattern?如何根据关键字/模式将一行文本拆分为多行?
【发布时间】:2015-08-25 14:06:48
【问题描述】:

我有一个 StringBuffer,它包含一个文件中的多行,所有这些都基于较大行的模式合并在一起以进行比较。进行更改后,我基本上需要撤消将这些行合并在一起以重新写入新的 StringBuffer 以重新写入文件原来的样子。

例如,文件包含这样的一行(都在一行上):

'macro NDD89 Start;Load; shortDescription; macro(continued) stepone;steptwo,do stuff macro(continued) stepthree;more steps; do stuff macro(continued) finalstep'

我需要将该行重写为多行(在有字符串“macro(continued)”的地方拆分):

macro NDD89 Start;Load; shortDescription;
macro(continued) stepone;steptwo,do stuff
macro(continued) stepthree;more steps; do stuff
macro(continued) finalstep'

如果有帮助,这里是我的代码 sn-p,它执行将原始文件放在多行中的第一部分并将它们组合到组并写入新文件:

    StringBuffer sb = new StringBuffer();
    Buffer = new FileReader(C:/original.txt);
    BufferedReader User_File = new BufferedReader(Buffer);
    String Line_of_Text;
    while((Line_of_Text = User_File.readLine()) != null){
        if(Line_of_Text.startsWith("macro ")){
            sb.append("\n"+Line_of_Text);
        }else if(Line_of_Text.startsWith("macro(continued)")){
            sb.append(Line_of_Text);
        }else{
            sb.append(Line_of_Text+"\n");
        }
    }
    BufferedWriter OutFile = new BufferedWriter(new FileWriter("C:/newFile.txt"));
    OutFile.write(sb.toString());
    OutFile.flush();
    OutFile.close();

我一直坚持这样做有一段时间了,有什么建议/想法吗?

【问题讨论】:

    标签: java stringbuffer


    【解决方案1】:

    最简单的解决方案!

    String x =oneLine.replace(" macro", "\nmacro");
    

    【讨论】:

      【解决方案2】:

      希望String类的split()和contains()方法可以帮到你。

      参考:How to split a string in Java

      【讨论】:

        【解决方案3】:

        您可以在给定令牌上使用String#split 来拆分您的String

        示例

        String test = "'macro NDD89 Start;Load; shortDescription; macro(continued) stepone;steptwo,do stuff macro(continued) stepthree;more steps; do stuff macro(continued) finalstep'";
        //                       // splits into String array, by
        //                       //     | followed by...
        //                       //     |  | literal text
        //                       //     |  |   | double-escape on parenthesis, as
        //                       //     |  |   | this is a regex pattern
        String[] splitted = test.split("(?=macro\\(continued\\))");
        System.out.println("Raw:\r\n");
        // prints the raw array (hard to read, as inline)
        System.out.println(Arrays.toString(splitted)); 
        // creates a list from the array and print each element one per line, trimmed
        List<String> splittedList = Arrays.asList(splitted);
        System.out.println("\r\nNice:\r\n");
        for (String s: splittedList) {
            System.out.println(s.trim());
        }
        

        输出

        Raw:
        
        ['macro NDD89 Start;Load; shortDescription; , macro(continued) stepone;steptwo,do stuff , macro(continued) stepthree;more steps; do stuff , macro(continued) finalstep']
        
        Nice:
        
        'macro NDD89 Start;Load; shortDescription;
        macro(continued) stepone;steptwo,do stuff
        macro(continued) stepthree;more steps; do stuff
        macro(continued) finalstep'
        

        【讨论】:

          【解决方案4】:

          问题是你有一个没有行的大字符串。您可以通过输入System.out.println(Line_of_Text); 来测试您在if(Line_of_Text.startsWith("macro ")){ 上方的内容。那就是你可以看到哪里出了问题。

          我会将您的附加代码更改为:

          String Line_of_Text = null;
          while ((Line_of_Text = User_File.readLine()) != null) {
              Line_of_Text = User_File.readLine();
              String[] splitted = Line_of_Text.split("macro ");
              for (String part : splitted) {
                  sb.append(part+"\n");
              }
          }
          

          这利用.split() 方法将您的字符串拆分为字符串数组。您可以使用该数组中的项目来追加它们。

          编辑:我看到您有多个分隔符。 .split() 需要一个正则表达式分隔符,所以你可以试试:Line_of_Text.split("macro|macro\\(continued\\)");

          【讨论】:

            【解决方案5】:

            快速修复 QuakCore 的答案。

            String breaker = "macro(continued)";
            String x =oneLine.replace(" "+breaker, "\n"+breaker);
            

            【讨论】:

              猜你喜欢
              • 2013-07-14
              • 1970-01-01
              • 2017-09-27
              • 1970-01-01
              • 1970-01-01
              • 2022-01-18
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多