Guest User

Untitled

a guest
Oct 22nd, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. private static boolean containsOnly(String input, char ch) {
  2. if (input.isEmpty())
  3. return false;
  4. for (int i = 0; i < input.length(); i++)
  5. if (input.charAt(i) != ch)
  6. return false;
  7. return true;
  8. }
  9.  
  10. System.out.println(containsOnly("++++", '+')); // prints: true
  11. System.out.println(containsOnly("----", '+')); // prints: false
  12. System.out.println(containsOnly("+-+-", '+')); // prints: false
  13.  
  14. // escape special character '+'
  15. input.matches("\++")
  16.  
  17. // '+' not special in a character class
  18. input.matches("[+]+")
  19.  
  20. // if "+" is dynamic value at runtime, use quote() to escape for you,
  21. // then use a repeating non-capturing group around that
  22. input.matches("(?:" + Pattern.quote("+") + ")+")
  23.  
  24. ^ #From start of string
  25. [^+]* #Match 0 or more non plus characters
  26. + #Match 1 plus character
  27. [^+]* #Match 0 or more non plus characters
  28. $ #End of string
  29.  
  30. input.matches("[^\+]*?+[^\+]*")
Add Comment
Please, Sign In to add comment