/** * Like String.indexOf() except that it will look for any of the * characters in 'chars' (similar to C's strpbrk) */ public static int indexOfChars(String str, String chars) { return indexOfChars(str, chars, 0); }
/** * Like String.indexOf() except that it will look for any of the * characters in 'chars' (similar to C's strpbrk) */ public static int indexOfChars(String str, String chars) { return indexOfChars(str, chars, 0); }
/** * Like String.indexOf() except that it will look for any of the * characters in 'chars' (similar to C's strpbrk) */ public static int indexOfChars(String str, String chars) { return indexOfChars(str, chars, 0); }
/** * Counts the number of (not necessarily distinct) characters in the * string that also happen to be in 'chars' */ public static int numSharedChars(final String str, final String chars) { if (str == null || chars == null) { return 0; } int total = 0, pos = -1; while ((pos = indexOfChars(str, chars, pos + 1)) != -1) { total++; } return total; }
/** * Counts the number of (not necessarily distinct) characters in the * string that also happen to be in 'chars' */ public static int numSharedChars(final String str, final String chars) { if (str == null || chars == null) { return 0; } int total = 0, pos = -1; while ((pos = indexOfChars(str, chars, pos + 1)) != -1) { total++; } return total; }
/** * Counts the number of (not necessarily distinct) characters in the * string that also happen to be in 'chars' */ public static int numSharedChars(final String str, final String chars) { if (str == null || chars == null) { return 0; } int total = 0, pos = -1; while ((pos = indexOfChars(str, chars, pos + 1)) != -1) { total++; } return total; }
/** * Like String.replace() except that it accepts any number of old chars. * Replaces any occurrances of 'oldchars' in 'str' with 'newchar'. * Example: replaceChars("Hello, world!", "H,!", ' ') returns " ello world " */ public static String replaceChars(String str, String oldchars, char newchar) { int pos = indexOfChars(str, oldchars); if (pos == -1) { return str; } StringBuilder buf = new StringBuilder(str); do { buf.setCharAt(pos, newchar); pos = indexOfChars(str, oldchars, pos + 1); } while (pos != -1); return buf.toString(); }
/** * Like String.replace() except that it accepts any number of old chars. * Replaces any occurrances of 'oldchars' in 'str' with 'newchar'. * Example: replaceChars("Hello, world!", "H,!", ' ') returns " ello world " */ public static String replaceChars(String str, String oldchars, char newchar) { int pos = indexOfChars(str, oldchars); if (pos == -1) { return str; } StringBuilder buf = new StringBuilder(str); do { buf.setCharAt(pos, newchar); pos = indexOfChars(str, oldchars, pos + 1); } while (pos != -1); return buf.toString(); }
/** * Like String.replace() except that it accepts any number of old chars. * Replaces any occurrances of 'oldchars' in 'str' with 'newchar'. * Example: replaceChars("Hello, world!", "H,!", ' ') returns " ello world " */ public static String replaceChars(String str, String oldchars, char newchar) { int pos = indexOfChars(str, oldchars); if (pos == -1) { return str; } StringBuilder buf = new StringBuilder(str); do { buf.setCharAt(pos, newchar); pos = indexOfChars(str, oldchars, pos + 1); } while (pos != -1); return buf.toString(); }
/** * Removes all characters from 'str' that are not in 'retainChars'. * Example: retainAllChars("Hello, world!", "lo") returns "llool" */ public static String retainAllChars(String str, String retainChars) { int pos = indexOfChars(str, retainChars); if (pos == -1) { return ""; } StringBuilder buf = new StringBuilder(); do { buf.append(str.charAt(pos)); pos = indexOfChars(str, retainChars, pos + 1); } while (pos != -1); return buf.toString(); }
/** * Removes all characters from 'str' that are not in 'retainChars'. * Example: retainAllChars("Hello, world!", "lo") returns "llool" */ public static String retainAllChars(String str, String retainChars) { int pos = indexOfChars(str, retainChars); if (pos == -1) { return ""; } StringBuilder buf = new StringBuilder(); do { buf.append(str.charAt(pos)); pos = indexOfChars(str, retainChars, pos + 1); } while (pos != -1); return buf.toString(); }
/** * Removes all characters from 'str' that are not in 'retainChars'. * Example: retainAllChars("Hello, world!", "lo") returns "llool" */ public static String retainAllChars(String str, String retainChars) { int pos = indexOfChars(str, retainChars); if (pos == -1) { return ""; } StringBuilder buf = new StringBuilder(); do { buf.append(str.charAt(pos)); pos = indexOfChars(str, retainChars, pos + 1); } while (pos != -1); return buf.toString(); }
/** * Remove any occurrances of 'oldchars' in 'str'. * Example: removeChars("Hello, world!", ",!") returns "Hello world" */ public static String removeChars(String str, String oldchars) { int pos = indexOfChars(str, oldchars); if (pos == -1) { return str; } StringBuilder buf = new StringBuilder(); int start = 0; do { buf.append(str.substring(start, pos)); start = pos + 1; pos = indexOfChars(str, oldchars, start); } while (pos != -1); if (start < str.length()) { buf.append(str.substring(start)); } return buf.toString(); }
/** * Remove any occurrances of 'oldchars' in 'str'. * Example: removeChars("Hello, world!", ",!") returns "Hello world" */ public static String removeChars(String str, String oldchars) { int pos = indexOfChars(str, oldchars); if (pos == -1) { return str; } StringBuilder buf = new StringBuilder(); int start = 0; do { buf.append(str.substring(start, pos)); start = pos + 1; pos = indexOfChars(str, oldchars, start); } while (pos != -1); if (start < str.length()) { buf.append(str.substring(start)); } return buf.toString(); }
/** * Remove any occurrances of 'oldchars' in 'str'. * Example: removeChars("Hello, world!", ",!") returns "Hello world" */ public static String removeChars(String str, String oldchars) { int pos = indexOfChars(str, oldchars); if (pos == -1) { return str; } StringBuilder buf = new StringBuilder(); int start = 0; do { buf.append(str.substring(start, pos)); start = pos + 1; pos = indexOfChars(str, oldchars, start); } while (pos != -1); if (start < str.length()) { buf.append(str.substring(start)); } return buf.toString(); }