package modern.challenge; import java.util.regex.Pattern; import java.util.stream.Collectors; public final class Strings { private Strings() { throw new AssertionError("Cannot be instantiated"); } public static String removeCharacterV1(String str, char ch) { if (str == null || str.isEmpty()) { // or throw IllegalArgumentException return ""; } StringBuilder sb = new StringBuilder(); char[] chArray = str.toCharArray(); for (char c : chArray) { if (c != ch) { sb.append(c); } } return sb.toString(); } public static String removeCharacterV2(String str, char ch) { if (str == null || str.isEmpty()) { // or throw IllegalArgumentException return ""; } return str.replaceAll(Pattern.quote(String.valueOf(ch)), ""); } public static String removeCharacterV3(String str, char ch) { if (str == null || str.isEmpty()) { // or throw IllegalArgumentException return ""; } return str.chars() .filter(c -> c != ch) .mapToObj(c -> String.valueOf((char) c)) .collect(Collectors.joining()); } public static String removeCharacterV4(String str, String ch) { if (str == null || ch == null || str.isEmpty() || ch.isEmpty()) { // or throw IllegalArgumentException return ""; } if (ch.codePointCount(0, ch.length()) != 1) { return ""; // there is more than 1 Unicode character in the given String } int codePoint = ch.codePointAt(0); return str.codePoints() .filter(c -> c != codePoint) .mapToObj(c -> String.valueOf(Character.toChars(c))) .collect(Collectors.joining()); } }