Representing ^A (Unicode \u0001) correctly in Java when passed as argument
The argument you pass from command line is not actually unicode character but it's a String
of unicode character which is escaped with \
. Ultimately, your String will become \\u0001
and that's why it is printing \u0001
. Same way, if you enter \
as a command line argument it will become \\
to escape your backward slash.
While the String
you have declared in main is actually unicode character.
String escapedstring = "\\u0001";//in args[0]
String unicodeChar = "\u0001";// in str
So, now you want \\u0001
to be converted into \u0001
and there are lot of ways to achieve that. i.e. you can use StringEscapeUtils#unescapeJava
method of utility or you can also try following way.
String str = "\\u0001";
char unicodeChar = (char) Integer.parseInt(str.substring(2));
System.out.println(unicodeChar);
NOTE : You can find other ways to convert unicode String to unicode characters in following question.(Already provided in comment by Marcinek
)
vinayak_narune
I am passionate about big data and related technologies, have extensive work experience in Java, Hadoop ecosystem.
Updated on June 04, 2022Comments
-
vinayak_narune almost 2 years
public class TestU { public static void main(String[] args) { String str = "\u0001"; System.out.println("str-->"+str); System.out.println("arg[0]-->"+args[0]); } }
Output :
str-->^A arg[0]-->\u0001
I am passing
arg[0]
as\u0001
I executed this code in linux, the command line variable is not taken as unicode special character.