/** * <p>Takes an argument in set-syntax, see evaluateSet, * and returns the number of characters present in the specified string.</p> * * <p>An example would be:</p> * <ul> * <li>count("hello", {"c-f", "o"}) returns 2.</li> * </ul> * * @see CharSet#getInstance(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 (StringUtils.isEmpty(str) || ArrayUtils.isEmpty(set)) { return 0; } CharSet chars = CharSet.getInstance(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; }
/** * <p>Constructs a new CharSet using the set syntax. * Each string is merged in with the set.</p> * * @param setStrs Strings to merge into the initial set, may be null * @return a CharSet instance * @since 2.4 */ public static CharSet getInstance(String[] setStrs) { if (setStrs == null) { return null; } return new CharSet(setStrs); }
/** * <p>Constructs a new CharSet using the set syntax.</p> * * @param setStr the String describing the set, may be null * @since 2.0 */ protected CharSet(String setStr) { super(); add(setStr); }
/** * Implementation of delete and keep * * @param str String to modify characters within * @param set String[] set of characters to modify * @param expect whether to evaluate on match, or non-match * @return modified String */ private static String modify(String str, String[] set, boolean expect) { CharSet chars = CharSet.getInstance(set); StrBuilder buffer = new StrBuilder(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(); }
/** * <p>Creates a <code>CharSet</code> instance which allows a certain amount of * set logic to be performed.</p> * <p>The syntax is:</p> * <ul> * <li>"aeio" which implies 'a','e',..</li> * <li>"^e" implies not e.</li> * <li>"ej-m" implies e,j->m. e,j,k,l,m.</li> * </ul> * * <pre> * CharSetUtils.evaluateSet(null) = null * CharSetUtils.evaluateSet([]) = CharSet matching nothing * CharSetUtils.evaluateSet(["a-e"]) = CharSet matching a,b,c,d,e * </pre> * * @param set the set, may be null * @return a CharSet instance, <code>null</code> if null input * @deprecated Use {@link CharSet#getInstance(String[])}. * Method will be removed in Commons Lang 3.0. */ public static CharSet evaluateSet(String[] set) { if (set == null) { return null; } return new CharSet(set); }
/** * <p>Constructs a new CharSet using the set syntax. * Each string is merged in with the set.</p> * * @param set Strings to merge into the initial set * @throws NullPointerException if set is <code>null</code> */ protected CharSet(String[] set) { super(); int sz = set.length; for (int i = 0; i < sz; i++) { add(set[i]); } }
return str; CharSet chars = CharSet.getInstance(set); StrBuilder buffer = new StrBuilder(str.length()); char[] chrs = str.toCharArray(); for (int i = 0; i < sz; i++) { ch = chrs[i]; if (chars.contains(ch)) { if ((ch == lastChar) && (i != 0)) { continue;
return (CharSet) set; return new CharSet(setStr);
/** * <p>Constructs a new CharSet using the set syntax.</p> * * @param setStr the String describing the set, may be null * @since 2.0 */ protected CharSet(String setStr) { super(); add(setStr); }
public void setName( String name ) { CharSet illegal = CharSet.getInstance( ":/\\\"' " ); for ( int i = 0; i < name.length(); i++ ) { char c = name.charAt( i ); if ( illegal.contains( c ) ) { throw new IllegalArgumentException( "Name cannot contain characters from the set [" + illegal + "]" ); } } this.name = name; }
/** * <p>Constructs a new CharSet using the set syntax. * Each string is merged in with the set.</p> * * @param setStrs Strings to merge into the initial set, may be null * @return a CharSet instance * @since 2.4 */ public static CharSet getInstance(String[] setStrs) { if (setStrs == null) { return null; } return new CharSet(setStrs); }
/** * <p>Constructs a new CharSet using the set syntax.</p> * * @param setStr the String describing the set, may be null * @since 2.0 */ protected CharSet(String setStr) { super(); add(setStr); }
public void setName( String name ) { CharSet illegal = CharSet.getInstance( ":/\\\"' " ); for ( int i = 0; i < name.length(); i++ ) { char c = name.charAt( i ); if ( illegal.contains( c ) ) { throw new IllegalArgumentException( "Name cannot contain characters from the set [" + illegal + "]" ); } } this.name = name; }
/** * <p>Constructs a new CharSet using the set syntax. * Each string is merged in with the set.</p> * * @param setStrs Strings to merge into the initial set, may be null * @return a CharSet instance * @since 2.4 */ public static CharSet getInstance(String[] setStrs) { if (setStrs == null) { return null; } return new CharSet(setStrs); }