fork(13) download
  1. import java.util.regex.Pattern;
  2. import java.util.stream.Collectors;
  3. import java.util.Scanner;
  4.  
  5. class StringCap {
  6.  
  7. public static void main(String[] args) {
  8. Scanner scanner = new Scanner(System.in);
  9. while (scanner.hasNextLine()) {
  10. System.out.println(titleize(scanner.nextLine()));
  11. }
  12. }
  13.  
  14. /**
  15. * Titleize a string. Takes a string and returns a new string where all words
  16. * have had their first letter title cased. If the letter is already title case
  17. * or is not a cased letter (like a number), it will be passed through.
  18. * Leading, trailing, and all other whitespace is preserved.
  19. *
  20. * This method is not as robust as titleize in Rails. It does not do any magic
  21. * like breaking up MashedTogetherWords or replacing_underscores.
  22. *
  23. * This will blow up if null is passed in.
  24. */
  25. private static final Pattern bound = Pattern.compile("\\b(?=\\w)");
  26.  
  27. private static final String ucFirst(String input) {
  28. return input.isEmpty() ? input : (input.substring(0, 1).toUpperCase() + input.substring(1));
  29. };
  30.  
  31. public static String titleize(final String input) {
  32. return bound.splitAsStream(input)
  33. .map(StringCap::ucFirst)
  34. .collect(Collectors.joining());
  35.  
  36. }
  37.  
  38.  
  39. }
  40.  
Success #stdin #stdout 0.29s 321472KB
stdin
this is a 'common-line' string and another.
This is 'quoted'
This has-joined-words
This should not 1800not1337
stdout
This Is A 'Common-Line' String And Another.
This Is 'Quoted'
This Has-Joined-Words
This Should Not 1800not1337