开发者

get string from between characters

i have a String

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 "

and I have to parse it and return 4 strings (each in a variable) as follows:

a=12:32:02

b=Demonstration

 c=" "

 开发者_如何学运维d=" "

a=12:32:05

b=Demonstration

c=Mémoire centrale 

d=Inialisation

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

my code :

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


}

but it won't work

and the 5 first lines of S are not in consideration (i don't know how to do it)


If you wish to ignore the first five lines, call S.readLine() five times. Each time you call it, it will read (and discard) a line of text from the input stream.


Is this not enough:

str.split("|");

?

And

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


Your string s is structured in multiple lines, so you have to (1) split on \n, (2) skip the first 5 rows, (3) split each row on \\|, and (4) assign variables when data is available. Here is a sample code that should give you an idea

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

}

and that produces the following output:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜