получить строку между символами - PullRequest
0 голосов
/ 11 апреля 2011

У меня есть строка

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


}

но это не сработает

и 5 первых строк S не рассматриваются (я не знаю, как это сделать)

Ответы [ 3 ]

0 голосов
/ 11 апреля 2011

Разве этого недостаточно:

str.split("|");

И

int counter = 0;
while(...){
    counter += 1;
    if (counter<=5) continue;
    ...
}
0 голосов
/ 11 апреля 2011

Ваша строка 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
0 голосов
/ 11 апреля 2011

Если вы хотите игнорировать первые пять строк, позвоните S.readLine() пять раз.Каждый раз, когда вы вызываете его, он читает (и удаляет) строку текста из входного потока.

...