Java Common String Operations Summary

Source: Internet
Author: User

1. String to ASCII code

    public static string Stringtoascii (string value) {        StringBuffer SBU = new StringBuffer ();        char[] chars = Value.tochararray ();        for (int i = 0; i < chars.length; i++) {            if (I <= chars.length-1) {                int II = chars[i];                Sbu.append (integer.tohexstring (ii));            } else {                sbu.append ((int) chars[i]);            }        }        return sbu.tostring ();    }

2. ASCII transcoding string

    public static string Asciitostring (string value) {        if ("". Equals (value) | | Value = = null) {            return "";        }        StringBuffer sbuffer = new StringBuffer ();        char[] chars = Value.tochararray ();        for (int i = 0; i < chars.length; i = i + 2) {            if (I < chars.length-1) {                int II = integer.valueof (                    Ch Aracter.tostring (Chars[i]) + character.tostring (chars[i + 1]);                Sbuffer.append ((char) (ii));            }        }        return sbuffer.tostring ();    }

3. Input Flow byte array

    public static String Inputstream2byte (InputStream inputstream) throws IOException {        Bytearrayoutputstream bos = new B Ytearrayoutputstream ();        byte[] buffer = new byte[1024];        int len =-1;        while (len = inputstream.read (buffer))! =-1) {            bos.write (buffer, 0, Len);        }        Bos.close ();        return new String (Bos.tobytearray (), "UTF-8");    }

4. Enter the flow string

    public static String inputstreamtostring (InputStream in) throws Exception {                Bytearrayoutputstream outstream = new by Tearrayoutputstream ();        byte[] data = new byte[1024];        int count =-1;        while ((count = in.read (data, 0, 2048))! =-1)            outstream.write (data, 0, count);                data = null;        return new String (Outstream.tobytearray ());    }

 

5. Determine if the input contains Chinese

    public static Boolean Ischinese (char c) {        Character.unicodeblock UB = Character.UnicodeBlock.of (c);        if (UB = = Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS | | ub = = Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS                | | ub = = Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A | | ub = = Character.UnicodeBlock.CJK_UNIFIED_ Ideographs_extension_b                | | ub = = Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION | | ub = = Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS                | | ub = = Character.UnicodeBlock.GENERAL_PUNCTUATION) {            return true;        }        return false;    }     public static Boolean Ischinese (String strName) {        char[] ch = strname.tochararray ();        for (int i = 0; i < ch.length; i++) {            char c = ch[i];            if (Ischinese (c)) {                return true;            }        }        return false;    }

  

6. Verify the legitimacy of the subnet mask: After the subnet mask is converted to binary, it cannot be all 1, nor all 0, must be contiguous 1 or successive 0, that is, 1 and 0 cannot alternate.

Implementation method, the first 4 integer number of fields in accordance with each eight-bit splicing, soft after conversion to binary display, using regular expressions to match.

    public static Boolean Checkmask (String maskstr) {        string[] ips = maskstr.split ("\ \.");          String binaryval = "";          for (int i = 0; i < ips.length; i++)          {              String binarystr = integer.tobinarystring (Integer.parseint (ips[i])); 
   integer times = 8-binarystr.length ();                            for (int j = 0; J < times; J + +)              {                  binarystr = "0" +  binarystr;  Eight-bit, eight-bit merge required each time            }              binaryval + = binarystr;          }          Pattern Regxpattern = Pattern.compile ("^[1]*[0]*$");        if (Regxpattern.matcher (Binaryval). Matches ())          {              return true;          } else {            return false;        }    }

7. Check the legality of the IP and convert to an integer representation

    public static Boolean Validip (String IP) {if (IP = = null) {return false; } String Ip_regex = "\\b (?!                                                       \\d\\d\\d) \\d+|1\\d\\d|2[0-4]\\d|25[0-5] \ \. " + "((?!                                                       \\d\\d\\d) \\d+|1\\d\\d|2[0-4]\\d|25[0-5] \ \. " + "((?!                                                       \\d\\d\\d) \\d+|1\\d\\d|2[0-4]\\d|25[0-5] \ \. " + "((?!        \\d\\d\\d) (\\d+|1\\d\\d|2[0-4]\\d|25[0-5]) \\b ";        Pattern Ippattern = Pattern.compile (Ip_regex);        Matcher Matcher = Ippattern.matcher (IP);    return matcher.matches ();        public static Integer Ipstring2int (String strIp) {if (!validip (strIp)) return null;        int[] IP = new INT[4];        string[] ips = strip.split ("\ \");        Ip[0] = Integer.parseint (ips[0]);        IP[1] = Integer.parseint (ips[1]);        IP[2] = Integer.parseint (ips[2]);        IP[3] = Integer.parseint (ips[3]); ReturN (ip[0] <<) + (Ip[1] << +) + (Ip[2] << 8) + ip[3]; }

  

  

  

Java Common String Operations Summary

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.