java 删除字符串中的特定字符
2014-10-24 02:09
246 查看
/** * Delete any character in a given String. * @param inString the original String * @param charsToDelete a set of characters to delete. * E.g. "az\n" 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); }
相关文章推荐
- Java:在字符串中删除特定的字符:输入两个字符串,从第一字符串中删除第二个字符串中所有的字符。
- 每天一道算法题12 在字符串中删除特定的字符
- 在字符串中删除特定的字符
- [字符串]在字符串中删除特定的字符
- 在字符串中删除特定的字符
- 【算法20】在字符串中删除特定的字符
- 在字符串中删除特定的字符
- 在字符串中删除特定的字符
- 程序员面试题精选100题(36)-在字符串中删除特定的字符
- java删除字符串中的指定字符
- 在字符串中删除特定的字符
- 程序员面试100题之三十六,在字符串中删除特定的字符
- 在字符串中删除特定的字符
- 在字符串中删除特定的字符
- 程序员面试题精选100题(36)-在字符串中删除特定的字符
- (12) 转载:在字符串中删除特定的字符
- 在字符串中删除特定的字符
- 算法题4 在字符串中删除特定的字符
- 【每日面试题】在字符串中删除特定的字符
- Java中如何从一个字符串中删除指定字符