Java tutorial
//package com.java2s; //License from project: Apache License public class Main { public static String removeGreaterLessThan5(String numStr, boolean which) { StringBuilder builder = new StringBuilder(); for (char c : numStr.toCharArray()) { if (isGreaterThan5(c) ^ which) { builder.append(c); } } return builder.toString(); } private static boolean isGreaterThan5(char c) { return c == '5' || c == '6' || c == '7' || c == '8' || c == '9'; } }