if (s.length() >= 3) { for (int i = 0; i < s.length() - 2; i++) { char c = s.charAt(i); String desired = "" + c + c + c; String actual = s.substring(i, i + 3); if (desired.equals(actual)) { // It matched .. System.out.println("True"); } } }