【问题标题】:get string from between characters从字符之间获取字符串
【发布时间】:2011-04-11 10:51:21
【问题描述】:

我有一个字符串

S=

"Apprenant: User1


Temps    |Activité           |Sous Activité      |Action             

12:32:02 |Demonstration      |                   |

12:32:05 |Démonstration      |Mémoire centrale   |Inialisation

12:32:06 |Démonstration      |                   |Chargement

12:32:07 |Démonstration      |                   |Inst Suiv

12:32:11 |Manipulation       |Mémoire centrale   |

12:32:15 |Manipulation       |Unité de commande  |

12:32:17 |Manipulation       |Mémoire centrale   |

12:32:20 |Manipulation       |Mémoire centrale   |Vider la mémoire "

我必须解析它并返回 4 个字符串(每个字符串都在一个变量中),如下所示:

a=12:32:02

b=Demonstration

 c=" "

 d=" "

a=12:32:05

b=Demonstration

c=Mémoire centrale 

d=Inialisation

.............
................ until the end

我的代码:

while( S != null)
{
      str =S.readLine(); // the text is in a TextArea


     String splitted[] = str.split("\\|");

String a = splitted[0].trim();

String b = splitted[1].trim();

String c = splitted[2].trim();

String d = splitted[3].trim();

System.out.println("a="+a);

System.out.println("b="+b);

System.out.println("c="+c);

System.out.println("d="+d);


}

但它不会工作

并且S的前5行不在考虑范围内(我不知道怎么做)

【问题讨论】:

  • 这个问题与您之前接受答案的stackoverflow.com/questions/5604498/… 之间究竟是什么关系?
  • “它不会工作”是什么意思?它有什么作用,它与你想要它做什么有什么不同?您的问题真的是字符串拆分还是忽略了前五行?
  • 你需要解析前五行,还是可以忽略它们(连续使用S.readLine() 5次)?
  • 忽略前 5 行
  • 我怎么能忽略前 5 行????另一个问题是 a=" " ,我没有在我的代码中处理这种情况

标签: java algorithm string character


【解决方案1】:

如果您想忽略前五行,请致电S.readLine() 五次。每次调用它时,它都会从输入流中读取(并丢弃)一行文本。

【讨论】:

    【解决方案2】:

    这还不够吗:

    str.split("|");
    

    ?

    int counter = 0;
    while(...){
        counter += 1;
        if (counter<=5) continue;
        ...
    }
    

    【讨论】:

      【解决方案3】:

      您的字符串s 由多行构成,因此您必须 (1) 在 \n 上拆分,(2) 跳过前 5 行,(3) 在 \\| 上拆分每一行,以及 (4 ) 在数据可用时分配变量。这是一个示例代码,应该会给您一个想法

      public static void main(String[] args) {
      
          String s = 
              "Apprenant: User1\n" +
              "\n" +
              "\n" +
              "Temps    |Activité           |Sous Activité      |Action\n" +
              "\n" +
              "12:32:02 |Demonstration      |                   |\n" +
              "\n" +
              "12:32:05 |Démonstration      |Mémoire centrale   |Inialisation\n" +
              "\n" +
              "12:32:06 |Démonstration      |                   |Chargement\n" +
              "\n" +
              "12:32:07 |Démonstration      |                   |Inst Suiv\n" +
              "\n" +
              "12:32:11 |Manipulation       |Mémoire centrale   |\n" +
              "\n" +
              "12:32:15 |Manipulation       |Unité de commande  |\n" +
              "\n" +
              "12:32:17 |Manipulation       |Mémoire centrale   |\n" +
              "\n" +
              "12:32:20 |Manipulation       |Mémoire centrale   |Vider la mémoire\n";
      
          String[] rows = s.split("\n");
          for (int i = 5; i < rows.length; i++) {
              String[] sections = rows[i].split("\\|");
              if (sections.length > 1) {
                  String a = sections[0].trim();
                  String b = "";
                  String c = "";
                  String d = "";
                  if (sections.length == 2)
                      b = sections[1].trim();
                  if (sections.length == 3);
                      c = sections[2].trim();
                  if (sections.length == 4)
                      d = sections[3].trim();
                  System.out.println("a = " + a + "; b = " + b + 
                          "; c = " + c + "; d = " + d);
              }
          }
      
      }
      

      并产生以下输出:

      a = 12:32:02; b = ; c = ; d = 
      a = 12:32:05; b = ; c = Mémoire centrale; d = Inialisation
      a = 12:32:06; b = ; c = ; d = Chargement
      a = 12:32:07; b = ; c = ; d = Inst Suiv
      a = 12:32:11; b = ; c = Mémoire centrale; d = 
      a = 12:32:15; b = ; c = Unité de commande; d = 
      a = 12:32:17; b = ; c = Mémoire centrale; d = 
      a = 12:32:20; b = ; c = Mémoire centrale; d = Vider la mémoire
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-11-25
        • 1970-01-01
        • 2012-12-28
        相关资源
        最近更新 更多