// ----------------------------------------------------------------------- // Implementation of delete and keep private static String modify(String str, String[] set, boolean expect) { TSCharSet chars = evaluateSet(set); StringBuffer buffer = new StringBuffer(str.length()); char[] chrs = str.toCharArray(); int sz = chrs.length; for (int i = 0; i < sz; i++) { if (chars.contains(chrs[i]) == expect) { buffer.append(chrs[i]); } } return buffer.toString(); }
/** * Takes an argument in set-syntax, see evaluateSet, and returns the number of characters present * in the specified string. * * <p>An example would be: * * <ul> * <li>count("hello", {"c-f", "o"}) returns 2. * </ul> * * @see #evaluateSet(java.lang.String[]) for set-syntax. * @param str String to count characters in, may be null * @param set String[] set of characters to count, may be null * @return character count, zero if null string input */ public static int count(String str, String[] set) { if (str == null || str.length() == 0 || set == null || set.length == 0) { return 0; } TSCharSet chars = evaluateSet(set); int count = 0; char[] chrs = str.toCharArray(); int sz = chrs.length; for (int i = 0; i < sz; i++) { if (chars.contains(chrs[i])) { count++; } } return count; }
/** * Squeezes any repititions of a character that is mentioned in the supplied set. * * <p>An example is: * * <ul> * <li>squeeze("hello", {"el"}) => "helo" * </ul> * * @see #evaluateSet(java.lang.String[]) for set-syntax. * @param str the string to squeeze, may be null * @param set the character set to use for manipulation, may be null * @return modified String, <code>null</code> if null string input */ public static String squeeze(String str, String[] set) { if (str == null || str.length() == 0 || set == null || set.length == 0) { return str; } TSCharSet chars = evaluateSet(set); StringBuffer buffer = new StringBuffer(str.length()); char[] chrs = str.toCharArray(); int sz = chrs.length; char lastChar = ' '; char ch = ' '; for (int i = 0; i < sz; i++) { ch = chrs[i]; if (chars.contains(ch)) { if ((ch == lastChar) && (i != 0)) { continue; } } buffer.append(ch); lastChar = ch; } return buffer.toString(); }