Please, do a System.out.println("[" + args[i] + "]");
to see what java is receiving from the command line, as the \
character is special for the shell and aso are the |
and ~
chars (the last one expands to your home directory, which could be a problem)
You need to pass:
java foo_bar '~\|~'
(Java still needs a single \
this time to escape the vertical bar, as you are not writing a string literal for the java compiler but a simple string representing the internal representation of the above string literal, the \
character doesn't need to be escaped, as it is inside single quotes so it is passed directly to the java program) Any quoting (single or double quotes) suffices to avoid ~
expansion.
If you are passing
java foo_bar '~\\|~'
the shell will not assume the \
as a escaping character and will pass the equivalent to this String
literal:
String sa[] = s.split("~\\\\|~", -1); /* to escapes mean a literal escape */
(see that now the vertical bar doesn't have its special significance)
...which is far different (you meant this time: split on one ~\
sequence, this is, a ~
followed by a backslash, or just a single ~
character, and as there are no ~
s followed by a backslash, the second option was used. You should get:
1
|
Vijay
|
25
|
Pune
Which is the output you post.