Get string from between two characters

前端 未结 7 1177
情歌与酒
情歌与酒 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 04:49

    Alternatively, org.apache.commons.lang.StringUtils has about 14 variants of the split() method.

    0 讨论(0)
  • 2021-01-27 04:50

    Like in other answers I suggest you using split() method to separate your string but remeber to use trim if you won't have spaces after parts, like this:

    S= "10:21:35 |Manipulation       |Mémoire centrale   |MAJ Registre mémoire"    
    String splitted[] = S.split("\\|");
    String a = splitted[0].trim();
    String b = splitted[1].trim();
    ...
    
    0 讨论(0)
  • 2021-01-27 04:59

    You can also use string.substring and get the required output as

    string a =s.substring(0,8)
    

    Like this u can assign for the one you want

    0 讨论(0)
  • 2021-01-27 05:02

    You probably want this:

    String[] s = "10:21:35 |Manipulation |Mémoire centrale |MAJ Registre mémoire".split("\\|");
    

    There's also method trim() which removes trailing spaces from the strings.

    0 讨论(0)
  • 2021-01-27 05:03
    String s = " 10:21:35   |  Manipulation |  Mémoire centrale |   MAJ Registre mémoire   ";
    String[] split = s.trim().split("\\s*\\|\\s*",-1); //trim and split
    
    0 讨论(0)
  • 2021-01-27 05:07

    If the length of each column is varient, use the examples given here with the split method.

    However, if you have a fixed-sized file format substring will be a much better option. If you look at the implementation of substring (Java 5 and above if I recall correctly) - you can see that it has an O(1) to create the new strings, whereas split uses a regex which is time consuming.

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