zoukankan      html  css  js  c++  java
  • java实现的escape和Unescape

     


    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<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));
      }
    }

     

  • 相关阅读:
    SpringBoot+ElementUI实现通用文件下载请求(全流程图文详细教程)
    Java中将String格式的标准时间字符串转换为Date格式的方法
    ElementUI中el-upload传递额外参数为date类型时后台SpringBoot接收不到
    模式、框架、架构和平台的区别
    架构、框架、模式和平台
    “模式”与“方式”的区别
    JAVA发送HTTP请求方式
    http status状态码,readyState状态码
    区分网络请求时http和ajax请求
    HTTP请求方式中8种请求方法
  • 原文地址:https://www.cnblogs.com/houfeng/p/1631418.html
Copyright © 2011-2022 走看看