StringUtils.java 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package com.zipcodewilmington.streams.tools;
  2. /**
  3. * Created by leon on 5/24/17.
  4. * @ATTENTION_TO_STUDENTS You are FORBIDDEN from modifying this class
  5. */
  6. public class StringUtils {
  7. /**
  8. * @param string - String to capitalize
  9. * @return - input String with first character capitalized
  10. */
  11. public static String capitalizeFirstChar(String string) {
  12. String firstChar = new Character(string.charAt(0)).toString();
  13. return string.replaceFirst(firstChar, firstChar.toUpperCase());
  14. }
  15. /**
  16. * @param numberOfRepeats - number of times to repeat this string
  17. * @param val - value of string to repeat
  18. * @return - `personSequence` concatenated with itself `numberOfRepeats` times
  19. */
  20. public static String repeatString(int numberOfRepeats, String val) {
  21. StringBuffer sb = new StringBuffer();
  22. for (int i = 0; i < numberOfRepeats; i++) {
  23. sb.append(val);
  24. }
  25. return sb.toString();
  26. }
  27. /**
  28. * @param s the String to pad
  29. * @param n the padding amount
  30. * @return the padded-left String
  31. */
  32. public static String padLeft(Object s, int n) {
  33. return String.format("%1$" + n + "s", s);
  34. }
  35. /**
  36. * @param s the String to pad
  37. * @param n the padding amount
  38. * @return the padded-left String
  39. */
  40. public static String padRight(String s, int n) {
  41. return padLeft(s, -n);
  42. }
  43. /**
  44. * @param str string to check
  45. * @return true if `str` is a palindrome
  46. */
  47. public static boolean isPalindromeIgnoreCase(String str) {
  48. return new StringBuilder(str).reverse().toString().equalsIgnoreCase(str);
  49. }
  50. }