Java Escape address bar Chinese encoding problem solution
Value Meaning
B Backspace
F Form feed
N New line
R Carriage return
T Tab
Public class MainClass {
Public static void main (String [] arg ){
System. out. println ("B ");
System. out. println ("f ");
System. out. println ("n ");
System. out. println ("r ");
System. out. println ("t ");
}
}
Chinese url conversion
Class EscapeUnescape
{
Public static String escape (String src)
{
Int I;
Char j;
StringBuffer tmp = new StringBuffer ();
Tmp. ensureCapacity (src. length () * 6 );
For (I = 0; I <src. length (); I ++)
{
J = src. charAt (I );
If (Character. isDigit (j) | Character. isLowerCase (j) | Character. isUpperCase (j ))
Tmp. append (j );
Else
If (j <1, 256)
{
Tmp. append ("% ");
If (j <16)
Tmp. append ("0 ");
Tmp. append (Integer. toString (j, 16 ));
}
Else
{
Tmp. append ("% u ");
Tmp. append (Integer. toString (j, 16 ));
}
}
Return tmp. toString ();
}
Public static String unescape (String src)
{
StringBuffer tmp = new StringBuffer ();
Tmp. ensureCapacity (src. length ());
Int lastPos = 0, pos = 0;
Char ch;
While (lastPos <src. length ())
{
Pos = src. indexOf ("%", lastPos );
If (pos = lastPos)
{
If (src. charAt (pos + 1) = 'u ')
{
Ch = (char) Integer. parseInt (src. substring (pos + 2, pos + 6), 16 );
Tmp. append (ch );
LastPos = pos + 6;
}
Else
{
Ch = (char) Integer. parseInt (src. substring (pos + 1, pos + 3), 16 );
Tmp. append (ch );
LastPos = pos + 3;
}
}
Else
{
If (pos =-1)
{
Tmp. append (src. substring (lastPos ));
LastPos = src. length ();
}
Else
{
Tmp. append (src. substring (lastPos, pos ));
LastPos = pos;
}
}
}
Return tmp. toString ();
}
Public static void main (String [] args)
{
String tmp = "~! @ # $ % ^ & * () _ + | =-,./?> <; '] [{} "";
System. out. println ("testing escape:" + tmp );
Tmp = escape (tmp );
System. out. println (tmp );
System. out. println ("testing unescape:" + tmp );
System. out. println (unescape (tmp ));
}
}
Unicode encoding
Public class MainClass {
Public static void main (String [] arg ){
Char myCharacter = 'u0058 ';
System. out. println (myCharacter );
}
}