我有一个字符串
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个第一行(我不知道该怎么做)
答案 0 :(得分:0)
如果您想忽略前五行,请拨打S.readLine()
五次。每次调用它时,它都会从输入流中读取(并丢弃)一行文本。
答案 1 :(得分:0)
这还不够:
str.split("|");
和
int counter = 0;
while(...){
counter += 1;
if (counter<=5) continue;
...
}
答案 2 :(得分:0)
您的字符串s
由多行组成,因此您必须(1)拆分\n
,(2)跳过前5行,(3)拆分{{1并且(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);
}
}
}