Using Java to encode and decode Utf8url and to encode the way to Judge __java

Source: Internet
Author: User
Tags stringbuffer

More technical Articles please visit my personal blog http://www.rain1024.com when searching with a search engine, the characters you enter are usually encoded and then queried, this article detects the UTF8 character encoding in Java, The code that Utf8url encoded is hung out, changing the URL in the main function and running it directly when needed.

Package Com.rain.demo;
Import java.io.UnsupportedEncodingException;  public class utf8{/** * Utf8url encoded * @param s * @return/public static final string Utf8urlencode (String
    Text) {StringBuffer result = new StringBuffer ();
      for (int i = 0; i < text.length (); i++) {char c = text.charat (i);
      if (c >= 0 && C <= 255) {result.append (c);
        }else {byte[] b = new Byte[0];
        try {b = character.tostring (c). GetBytes ("UTF-8");
          }catch (Exception ex) {} for (int j = 0; J < B.length; J + +) {int k = b[j];
          if (k < 0) k = 256;
        Result.append ("%" + integer.tohexstring (k). toUpperCase ());
  }} return result.tostring (); /** * Utf8url decoding * @param text * @return/public static final String Utf8urldecode (String text) {S
    Tring result = "";
    int p = 0; if (Text!=null && text.length () >0) {TeXT = Text.tolowercase ();
      p = text.indexof ("%e");
      if (p = = 1) return text;
        while (P!=-1) {result = text.substring (0, p);
        Text = Text.substring (P, text.length ()); if (Text = = "" | |

        Text.length () < 9) return result;
        Result + = Codetoword (text.substring (0, 9));
        Text = text.substring (9, Text.length ());
      p = text.indexof ("%e");
  } return result + text;
    /** * UTF8URL encoded spin character * @param text * @return/private static final String Codetoword (String text) {
    String result;
      if (Utf8codecheck (text)) {byte[] code = new BYTE[3];
      Code[0] = (byte) (Integer.parseint (text.substring (1, 3), 16)-256);
      CODE[1] = (byte) (Integer.parseint (Text.substring (4, 6), 16)-256);
      CODE[2] = (byte) (Integer.parseint (Text.substring (7, 9), 16)-256);
      try {result = new String (code, "UTF-8");
      }catch (Unsupportedencodingexception ex) {result = null; }
    else {result = text;
  return result; /** * Encoding is valid * @param text * @return/private static Final Boolean Utf8codecheck (String text) {STR
    ing sign = "";
        if (Text.startswith ("%e")) for (int i = 0, p = 0; p!=-1; i++) {p = text.indexof ("%", p);
        if (P!=-1) p++;
      Sign + = P;
  Return Sign.equals ("147-1");
    /** * Determines whether UTF8URL encoding * @param text * @return/public static final Boolean isutf8url (String text) {
    Text = Text.tolowercase ();
    int p = text.indexof ("%");
    if (P!=-1 && text.length ()-p > 9) {text = Text.substring (p, p + 9);
  return Utf8codecheck (text);
    /** * Test * @param args/public static void main (string[] args) {String URL; url = "http://www.google.com/search?hl=zh-CN&newwindow=1&q=%E4%B8%AD%E5%9B%BD%E5%A4%A7%E7%99%BE%E7%A7%91 %e5%9c%a8%e7%ba%bf%e5%85%a8%e6%96%87%e6%a3%80%e7%b4%a2&btNg=%e6%90%9c%e7%b4%a2&lr= ";
    if (Chartools.isutf8url (URL)) {System.out.println (Chartools.utf8urldecode (URL));
    url = "http://www.google.com/search?hl=zh-cn&newwindow=1&q= China Encyclopedia online full-text Search &btng= searching &lr="; if (!
    Chartools.isutf8url (URL)) {System.out.println (Chartools.utf8urlencode (URL)); }
  }
}

More technical Articles please visit my personal blog http://www.rain1024.com

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.