M
michael.biden
I have a situation in which I am receiving a String from a non-java
system. The system that generates the String attempts to encode some
characters such a slash to unicode. However it encodes characters
using the percent sign rathern than the backslash.
Thus the String test-victorf becomes test%u002dvictorf. I'd love to
be able to simply replace the percent with a backslash, but it seems
that there is no way to dynamically insert the backslash like a
literal. For example:
public static void main (String args[]){
String user = "test%u002dvictof";
user = user.replace('%', '\\');
System.out.println(user);
}
Does not work. The output is test\002dvictorf.
So I tried to use a regular expression with a capturing parantheses:
public static void main (String args[]){
String user = "test%u002dvictof";
user = user.replaceAll("%u([a-f | A-F | 0-9][a-f | A-F | 0-9][a-f |
A-F | 0-9][a-f | A-F | 0-9])",
Character.toString((char)Integer.valueOf("$1", 16).intValue()) );
System.out.println(user);
}
Which generates a java.lang.NumberFormatException becuase the compiler
does not like the $1 at runtime. It seems that the $1 is being
interpretted literally. The real value of $1 at run time is '002d'
Any help is appreciated.
Thanks.
system. The system that generates the String attempts to encode some
characters such a slash to unicode. However it encodes characters
using the percent sign rathern than the backslash.
Thus the String test-victorf becomes test%u002dvictorf. I'd love to
be able to simply replace the percent with a backslash, but it seems
that there is no way to dynamically insert the backslash like a
literal. For example:
public static void main (String args[]){
String user = "test%u002dvictof";
user = user.replace('%', '\\');
System.out.println(user);
}
Does not work. The output is test\002dvictorf.
So I tried to use a regular expression with a capturing parantheses:
public static void main (String args[]){
String user = "test%u002dvictof";
user = user.replaceAll("%u([a-f | A-F | 0-9][a-f | A-F | 0-9][a-f |
A-F | 0-9][a-f | A-F | 0-9])",
Character.toString((char)Integer.valueOf("$1", 16).intValue()) );
System.out.println(user);
}
Which generates a java.lang.NumberFormatException becuase the compiler
does not like the $1 at runtime. It seems that the $1 is being
interpretted literally. The real value of $1 at run time is '002d'
Any help is appreciated.
Thanks.