zoukankan      html  css  js  c++  java
  • java 删除字符串中的特定字符

        /**
         * Delete any character in a given String.
         * @param inString the original String
         * @param charsToDelete a set of characters to delete.
         * E.g. "az
    " will delete 'a's, 'z's and new lines.
         * @return the resulting String
         */
        public static String deleteAny(String inString, String charsToDelete) {
            if (!hasLength(inString) || !hasLength(charsToDelete)) {
                return inString;
            }
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < inString.length(); i++) {
                char c = inString.charAt(i);
                if (charsToDelete.indexOf(c) == -1) {
                    sb.append(c);
                }
            }
            return sb.toString();
        }
        /**
         * Check that the given String is neither {@code null} nor of length 0.
         * Note: Will return {@code true} for a String that purely consists of whitespace.
         * @param str the String to check (may be {@code null})
         * @return {@code true} if the String is not null and has length
         * @see #hasLength(CharSequence)
         */
        public static boolean hasLength(String str) {
            return hasLength((CharSequence) str);
        }
        /**
         * Check that the given CharSequence is neither {@code null} nor of length 0.
         * Note: Will return {@code true} for a CharSequence that purely consists of whitespace.
         * <p><pre class="code">
         * StringUtils.hasLength(null) = false
         * StringUtils.hasLength("") = false
         * StringUtils.hasLength(" ") = true
         * StringUtils.hasLength("Hello") = true
         * </pre>
         * @param str the CharSequence to check (may be {@code null})
         * @return {@code true} if the CharSequence is not null and has length
         * @see #hasText(String)
         */
        public static boolean hasLength(CharSequence str) {
            return (str != null && str.length() > 0);
        }
  • 相关阅读:
    BZOJ 4032: [HEOI2015]最短不公共子串 (dp*3 + SAM)
    后缀自动机详解!
    BZOJ 3926: [Zjoi2015]诸神眷顾的幻想乡(广义后缀自动机 多串)
    BZOJ 3938 Robot
    [JSOI2008]Blue Mary开公司
    [ZJOI2017]树状数组
    [JSOI2015]非诚勿扰
    [HNOI2011]任务调度
    BZOJ 3680 吊打XXX
    POJ 3318 Matrix Multiplication
  • 原文地址:https://www.cnblogs.com/ghgyj/p/4047294.html
Copyright © 2011-2022 走看看