0

I want to split this string :

String line = "04/25/17|13:00|Line 0";

into ("04/25/17","13:00","Line 0");

I tried String[] parts[] = line.split("|"); and I've got (0,4,/,2,5,/,7,1,3,:,0,0,L,i,n,e,0) then I tried String[] parts[] = line.split("\\|"); I got the same result

Please help!

ververicka
  • 175
  • 1
  • 2
  • 11
  • 1
    [`line.split("\\|");` works.](https://ideone.com/LH7OZK) – Marvin Apr 25 '17 at 20:57
  • 1
    "I got the same result" only reason where this could happen is when you didn't save/recompile your code. – Pshemo Apr 25 '17 at 21:01
  • yes it Works thanks :) – ververicka Apr 25 '17 at 21:10
  • You are welcome. But I would say that you can free to delete this question since it looks like there was no real problem to begin with. People who didn't save/recompile their code will most likely not search for solution to their problem under "split on |" subject. – Pshemo Apr 26 '17 at 16:43

1 Answers1

-1

If your strings all having the same length you can work with substring():

String line = "04/25/17|13:00|Line 0";
String part1 = line.substring(0,7);
String part2 = line.substring(9,13);
String part3 = line.substring(15,line.length());
Patrick
  • 39
  • 4
  • 2
    This isn't dynamic. What if he wanted to represent the year using 4 digits? You'd have to change all the substring arguments, which isn't scalable. – Vince Apr 25 '17 at 21:00
  • I'm aware of that. Out of that reason i wrote "if your strings all having the same length" – Patrick Apr 25 '17 at 21:02
  • You can shorten `substring(15,line.length());` with simple `substring(15);` – Pshemo Apr 25 '17 at 21:03