Custom Encapsulation stringutils Common tool classes

Source: Internet
Author: User
 Custom Encapsulation StringUtils common tools classes 
Package com.demo.utils;
Import java.util.ArrayList;
Import java.util.List;

Import Java.util.Map; /** * String Manipulation tool class * @author Dongyangyang * @Date 2016/12/28 23:12 * @Version 1.0 * * */public class StringUtils {/
        * * First Letter lowercase * @param str * @return/public static string Firstchartolowercase (String str) {
        Char firstchar = str.charat (0);
            if (Firstchar >= ' A ' && firstchar <= ' Z ') {char[] arr = Str.tochararray ();
            Arr[0] + + = (' A '-' a ');
        return new String (arr);
    return str; /** * First Letter capitalized * @param str * @return/public static string Firstchartouppercase (String str
        ) {Char Firstchar = str.charat (0);
            if (Firstchar >= ' a ' && firstchar <= ' z ') {char[] arr = Str.tochararray ();
            Arr[0]-= (' A '-' a ');
        return new String (arr);
    return str;
}/** * To determine if NULL     * @param str * @return/public static Boolean IsEmpty (Final String str) {return (str = NULL ) ||
    (str.length () = = 0); /** * Determine if NOT NULL * @param str * @return/public static Boolean Isnotempty (Final String str)
    {return!isempty (str);
        /** * Determine if blank * @param str * @return/public static Boolean IsBlank (Final String str) {
        int strLen; if (str = null) | |
            ((StrLen = Str.length ()) = = 0))
        return true; for (int i = 0; i < StrLen i++) {if (!
            Character.iswhitespace (Str.charat (i)) {return false;
    } return true;  /** * Determine if it is not blank * @param str * @return/public static Boolean Isnotblank (Final String str)
    {return!isblank (str); /** * To determine if multiple strings are all empty * @param strings * @return/public static Boolean Isallempty (string..
        strings {if (strings = = null) {return true;
            for (String str:strings) {if (Isnotempty (str)) {return false;
    } return true; /** * To determine whether any of the strings are null * @param strings * @return/public static Boolean Ishasempty (Stri
        Ng ... strings) {if (strings = = null) {return true;
            for (String str:strings) {if (IsEmpty (str)) {return true;
    return false; /** * CheckValue is null or "" returns DefaultValue * @param checkvalue * @param defaultvalue * @ret URN */public static string IsEmpty (string CheckValue, string defaultvalue) {return IsEmpty (CheckValue) ?
    Defaultvalue:checkvalue; }/** * string is not null and is equal to other * @param str * @param other * @return/public STA Tic Boolean IsnoteMptyandequelsother (String str, string other) {if (IsEmpty (str)) {return false;
    Return to Str.equals (other); /** * string is not null and is not equal to other * @param str * @param other * @return/public St Atic boolean isnotemptyandnotequelsother (String str, string ... other) {if (IsEmpty (str)) {return FAL
        Se for (int i = 0; i < other.length i++) {if (Str.equals (other[i)) {return false
            ;
    } return true; /** * string is not equal to other * @param str * @param the other * @return/public static Boolean IsNot Equelsother (String str, string ... other) {for (int i = 0; i < other.length; i++) {if (Other[i].eq
            Uals (str)) {return false;
    } return true; /** * Judge string is not NULL * @param strings * @return/Public STATic Boolean isnotempty (String ... strings) {if (strings = null) {return false; for (String str:strings) {if (str = NULL | |
            '. Equals (Str.trim ()) {return false;
    } return true; /** * Comparison character story * @param value * @param equals * @return/public static Boolean equals (
        String value, String equals) {if (Isallempty (value, equals)) {return true;
    return value.equals (equals); /** * Compare Strings Unequal * @param value * @param equals * @return/public static Boolean IsNot
    Equals (string value, String equals) {return!equals (value, equals); public static string[] Split (string content, String separatorchars) {return Splitworker (content, separator
    Chars,-1, false); public static string[] Split (String str, string separatorchars, int max) {return SPLitworker (str, separatorchars, Max, false);

    public static final string[] Empty_string_array = new String[0]; private static string[] Splitworker (String str, string separatorchars, int max, Boolean preservealltokens) {if (s)
        TR = = null) {return null;
        int len = Str.length ();
        if (len = = 0) {return empty_string_array;
        } list<string> List = new arraylist<string> ();
        int sizePlus1 = 1;
        int i = 0, start = 0;
        Boolean match = false;
        Boolean lastmatch = false; if (Separatorchars = = null) {while (I < len) {if (Character.iswhitespace (Str.charat (i)))
                        {if (match | | preservealltokens) {lastmatch = true;
                            if (sizeplus1++ = = max) {i = Len;
                        Lastmatch = false;
                       } List.add (str.substring (Start, i));
                    Match = false;
                    start = ++i;
                Continue
                } lastmatch = false;
                Match = true;
            i++;
            } else if (separatorchars.length () = = 1) {Char Sep = separatorchars.charat (0);
                        while (I < len) {if (Str.charat (i) = = Sep) {if (match | | preservealltokens) {
                        Lastmatch = true;
                            if (sizeplus1++ = = max) {i = Len;
                        Lastmatch = false;
                        } list.add (Str.substring (Start, i));
                    Match = false;
                    start = ++i;
                Continue
                } lastmatch = false;
                Match = true;
  i++;          } else {while (I < len) {if (Separatorchars.indexof (Str.charat (i)) &G
                        t;= 0) {if (match | | preservealltokens) {lastmatch = true;
                            if (sizeplus1++ = = max) {i = Len;
                        Lastmatch = false;
                        } list.add (Str.substring (Start, i));
                    Match = false;
                    start = ++i;
                Continue
                } lastmatch = false;
                Match = true;
            i++; if (Match | | (Preservealltokens && lastmatch))
        {List.add (str.substring (Start, i));
    Return (string[]) List.toarray (Empty_string_array);
 /** * Eliminate escape character * @param str * @return/public static string EscapeXML (String str) {       if (str = null) return "";
        StringBuilder sb = new StringBuilder ();
            for (int i = 0; i < str.length (); ++i) {char c = str.charat (i);
            Switch (c) {case ' \u00ff ': Case ' \u0024 ': break;
                Case ' & ': Sb.append ("&amp;");
            Break
                Case ' < ': Sb.append ("&lt;");
            Break
                Case ' > ': Sb.append ("&gt;");
            Break
                Case ' \ ': Sb.append ("&quot;");
            Break
                Case ' \ ': Sb.append ("&apos;");
            Break
                Default:if (c >= ' \u0000 ' && C <= ' \u001f ') break;
                if (c >= ' \ue000 ' && C <= ' \uf8ff ') break;
     if (c >= ' \ufff0 ' && C <= ' \uffff ')               Break
                Sb.append (c);
            Break
    } return sb.tostring ();            /** * Converts characters of a particular pattern in a string to the corresponding value in map * * @param s * The string to be converted * @param map * Converts the required key-value pairs to the collection * @return converted string/public static string replace (string s, map<string, object> Map)
        {StringBuilder ret = new StringBuilder ((int) (s.length () * 1.5));
        int cursor = 0; for (int start, end; (Start = S.indexof ("${", cursor))!=-1 && (end = S.indexof ("}", start))!=-1;
            {Ret.append (s.substring (cursor, start)). Append (Map.get (s.substring (start + 2, end));
        Cursor = end + 1;
        } ret.append (s.substring (cursor, s.length ()));
    return ret.tostring ();
            public static string replace (string s, Object ... objs) {if (Objs = null | | objs.length = 0)
        return s;
            if (S.indexof ("{}") = = 1)return s;
        StringBuilder ret = new StringBuilder ((int) (s.length () * 1.5));
        int cursor = 0;
        int index = 0; for (int start; (Start = S.indexof ("{}", cursor))!=-1;)
            {Ret.append (s.substring (cursor, start));
            if (Index < objs.length) ret.append (Objs[index]);
            Else Ret.append ("{}");
            cursor = start + 2;
        index++;
        } ret.append (s.substring (cursor, s.length ()));
    return ret.tostring ();
     /** * String Formatting tool, parameters must be marked with a style such as {0}. The numbers in curly braces start at 0.
     * * @param source * @param params * The list of parameters to be replaced and the ToString () of each parameter is invoked when writing. * @return Replace the completed string.
     If the original string is empty or the argument is empty, the original string is returned directly. */public static string Replaceargs (String source, Object ... params) {if (params = null | | params.length = = 0 | | Source = NULL | |
        Source.isempty ()) {return source; } StringBuilder buff = new StringBuilder (source);
        StringBuilder temp = new StringBuilder ();
        int startIndex = 0;
        int endindex = 0;
        String param = null; for (int count = 0; count < params.length count++) {if (Params[count] = null) {param =
            Null
            else {param = params[count].tostring ();
            Temp.delete (0, Temp.length ());
            Temp.append ("{");
            Temp.append (count);
            Temp.append ("}");
                while (true) {StartIndex = Buff.indexof (temp.tostring (), endindex);
                if (StartIndex = = 1) {break;

                } endindex = StartIndex + temp.length (); Buff.replace (StartIndex, endindex, param = = null?)
            "": param);
            } startIndex = 0;
        Endindex = 0;
    return buff.tostring (); public static String Substringbefore (FinAl string s, final String separator {if (IsEmpty (s) | | | separator = NULL) {return s;
        } if (Separator.isempty ()) {return "";
        Final int pos = s.indexof (separator);
        if (POS < 0) {return s;
    Return s.substring (0, POS);  public static string Substringbetween (final string str, final string open, final string close) {if (str = = null | | Open = NULL | |
        Close = = NULL) {return null;
        Final int start = Str.indexof (open);
            If (Start!=-1) {final int end = Str.indexof (Close, start + open.length ());
            If (end!=-1) {return str.substring (start + open.length (), end);
    } return null;
            public static string Substringafter (final string str, final string separator) {if (IsEmpty (str)) {
        return str;
  } if (separator = null) {          Return "";
        Final int pos = str.indexof (separator);
        if (pos = = 1) {return "";
    Return str.substring (pos + separator.length ());  /** * converts to byte array * @param str * @return/public static String toString (byte[] bytes) {Try
        {return new String (bytes, "Utf-8");
        catch (Unsupportedencodingexception e) {return null;
      }/** * converted to byte array * @param str * @return/public static byte[] GetBytes (String str) {
          if (str!= null) {try {return str.getbytes ("Utf-8");
          catch (Unsupportedencodingexception e) {return null;
      }}else{return null; }
    }

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.