1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | package Torello.Java; import Torello.Java.Function.ToCharIntCharFunc; class CharArrToCharReplFunc { static String replace( final String s, final boolean ignoreCase, final char[] matchCharsInput, final ToCharIntCharFunc replaceFunction ) { char[] cArr = s.toCharArray(); if (ignoreCase) { // Make sure the 'var-args' are not changed. We must resort to copying // the input 'matchChars' array. char[] matchChars = new char[matchCharsInput.length]; // Set them to lower-case, as they are copied. for (int i=0; i < matchChars.length; i++) matchChars[i] = Character.toLowerCase(matchCharsInput[i]); // Do the replacement on a case-insensitive basis for (int i=0; i < cArr.length; i++) { char c = Character.toLowerCase(cArr[i]); for (int j=0; j < matchChars.length; j++) if (c == matchChars[j]) { cArr[i] = replaceFunction.apply(i, cArr[i]); break; } } } else // Do the replacement, case-sensitive. for (int i=0; i < cArr.length; i++) for (int j=0; j < matchCharsInput.length; j++) if (cArr[i] == matchCharsInput[j]) { cArr[i] = replaceFunction.apply(i, cArr[i]); break; } return new String(cArr); } } |