Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private static boolean containsOnly(String input, char ch) {
- if (input.isEmpty())
- return false;
- for (int i = 0; i < input.length(); i++)
- if (input.charAt(i) != ch)
- return false;
- return true;
- }
- System.out.println(containsOnly("++++", '+')); // prints: true
- System.out.println(containsOnly("----", '+')); // prints: false
- System.out.println(containsOnly("+-+-", '+')); // prints: false
- // escape special character '+'
- input.matches("\++")
- // '+' not special in a character class
- input.matches("[+]+")
- // if "+" is dynamic value at runtime, use quote() to escape for you,
- // then use a repeating non-capturing group around that
- input.matches("(?:" + Pattern.quote("+") + ")+")
- ^ #From start of string
- [^+]* #Match 0 or more non plus characters
- + #Match 1 plus character
- [^+]* #Match 0 or more non plus characters
- $ #End of string
- input.matches("[^\+]*?+[^\+]*")
Add Comment
Please, Sign In to add comment