String 常用方法實現總結(一)

來源:互聯網
上載者:User

標籤:string   util   常用演算法   reverse   remove   

</pre><p>1. reverse</p><p><pre name="code" class="java">   /**     * @Description: reverse a string.     * @param str the String to reverse, may be null     * @return reversedStr the reversed String, null if null String input     */    public static String reverse(String str) {        if (isEmpty(str)) {            return str;        }        return new StringBuilder(str).reverse().toString();    }

2 . remove

    /**     * @Description: Removes all occurrences of a substring from within the source string.     * @param str the source String to search     * @param remove the String to search for and remove     * @return the substring with the string removed if found, null if null String input     */    public static String remove(String str, String remove) {        if (isEmpty(str) || isEmpty(remove)) {            return str;        }        return str.replace(remove, "");    }


3. startsWithIgnoreCase

    /**     *      * @Title: startsWithIgnoreCase     * @Description: check if a string starts with a specified prefix.     * @param str input value     * @param prefix prefix value     * @return true if the string starts with the prefix, case insensitive, or both null, blank     */    public static boolean startsWithIgnoreCase(String str, String prefix) {        if (str == null || prefix == null) {            return (str == null && prefix == null);        }        if (prefix.length() > str.length()) {            return false;        }        return str.toUpperCase().startsWith(prefix.toUpperCase());    }

4. isAllAlphas

   /**     * whether value does not contain number(s).     *      * @Title: isAllAlphas     * @param value the source to be handled     * @return boolean     */    public static boolean isAllAlphas(String value) {        // if input parameter is null, just return        if (value == null) {            return false;        }        for (int i = 0; i < value.length(); i++) {            if (!(Character.isLetter(value.charAt(i)))) {                return false;            }        }        return true;    }

5.  isNumeric

  /**     * check if the CharSequence contains only unicode digits.     *      * @Title: isNumber     * @param str input str     * @return true or false     */    public static boolean isNumeric(String str) {        // if input parameter is null, just return false        if (isEmpty(str)) {            return false;        }        for (int i = 0; i < str.length(); i++) {            // if there is a alpha, return false            if (!Character.isDigit(str.charAt(i))) {                return false;            }        }                return true;    }



String 常用方法實現總結(一)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.