Get string from between two characters

前端 未结 7 1179
情歌与酒
情歌与酒 2021-01-27 04:28

I need to get string from between two characters. I have this

S= \"10:21:35 |Manipulation       |Mémoire centrale   |MAJ Registre mémoire\"

a

相关标签:
7条回答
  • 2021-01-27 05:08

    There's String#split. Since it accepts a regular expression string, and | is a special character in regular expressions, you'll need to escape it (with a backslash). And since \ is a special character in Java string literals, you'll need to escape it, too, which people sometimes find confusing. So given:

    String S = "10:21:35 |Manipulation |Mémoire centrale |MAJ Registre mémoire";
    

    then

    String[] parts = S.split("\\|");
    int index;
    for (index = 0; index < parts.length; ++index) {
        System.out.println(parts[index]);
    }
    

    would output

    10:21:35 
    Manipulation 
    Mémoire centrale 
    MAJ Registre mémoire

    (With the trailing spaces on the first three bits; trim those if necessary.)

    0 讨论(0)
提交回复
热议问题