Practice with string and loop

StringParser.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /**
  2. * An introduction to Strings and String methods.
  3. *
  4. * @author Wilhem Alcivar
  5. */
  6. public class StringParser
  7. {
  8. /**
  9. * Takes a String and returns that String with all characters uppercased.
  10. * E.G. cat would become CAT. dOnUt would become DONUT.
  11. *
  12. * @param s
  13. * @return String
  14. */
  15. public static String upperCaseString(String s)
  16. {
  17. return s.toUpperCase();
  18. }
  19. /**
  20. * Takes a String and returns that String with all characters lowercased.
  21. * E.G. MOUSE would become mouse. dOnUt would become donut.
  22. *
  23. * @param s
  24. * @return String
  25. */
  26. public static String lowerCaseString(String s) {
  27. return s.toLowerCase();
  28. }
  29. /**
  30. * Takes a String and returns the first character of that string.
  31. * E.G. cat would return c. Embark would return E.
  32. *
  33. * @param s
  34. * @return String
  35. */
  36. public static Character getFirstCharacter(String s) {
  37. return s.charAt(0);
  38. }
  39. /**
  40. * Takes a String and returns the character at index n of that string.
  41. * E.G. cat, 2 would return t. Embark, 4 would return r.
  42. *
  43. * @param s
  44. * @param n
  45. * @return String
  46. */
  47. public static Character getNthCharacter(String s, Integer n) {
  48. return s.charAt(n);
  49. }
  50. /**
  51. * Takes a String and returns that string with the first character uppercased.
  52. * E.G. cat would return Cat. cofFee would return CofFee.
  53. *
  54. * @param s
  55. * @return String
  56. */
  57. public static String upperCaseFirstCharacter(String s) {
  58. return s.substring(0, 1).toUpperCase() + s.substring(1);
  59. }
  60. /**
  61. * Takes a String and returns that string with the first character of each word in it uppercased
  62. * and then joined.
  63. * E.G. dog whistle would return DogWhistle. adjuNCT pRoFessOR would return AdjuctProfessor.
  64. *
  65. * @param s
  66. * @return String
  67. */
  68. public static String camelCaseString(String s) {
  69. s = s.toLowerCase();
  70. int blankIndex = s.indexOf(" ")+1;
  71. String camelCase = s.substring(0,1).toUpperCase() + s.substring(1, blankIndex) +
  72. s.substring(blankIndex, blankIndex+1).toUpperCase() + s.substring(blankIndex+1);
  73. return camelCase.replace(" ","");
  74. }
  75. /**
  76. * Takes a String and returns that string with each character lowercased
  77. * and then joined with an underscore
  78. * E.G. dog whistle would return dog_whistle. adjuNCT pRoFessOR would return adjuct_professor.
  79. *
  80. * @param s
  81. * @return String
  82. */
  83. public static String snakeCaseString(String s) {
  84. return s.toLowerCase().replace(" ", "_");
  85. }
  86. /**
  87. * Takes a String and returns the length of that string
  88. * E.G. cat would return 3. extra would return 5.
  89. *
  90. * @param s
  91. * @return String
  92. */
  93. public static Integer getLength(String s) {
  94. return s.length();
  95. }
  96. /**
  97. * Takes in two strings and returns true if they are equal
  98. * E.G. example and shelf would return false.
  99. * cat and CaT would return false.
  100. * Dog and Dog would return true
  101. *
  102. * @param s1
  103. * @param s2
  104. * @return String
  105. */
  106. public static Boolean isEqual(String s1, String s2) {
  107. return s1.equals(s2);
  108. }
  109. /**
  110. * Takes in two strings and returns true if they are equal
  111. * E.G. example and shelf would return false.
  112. * cat and CaT would return true.
  113. * Dog and Dog would return true
  114. *
  115. * @param s1
  116. * @param s2
  117. * @return String
  118. */
  119. public static Boolean isEqualIgnoreCase(String s1, String s2) {
  120. return s1.equalsIgnoreCase(s2);
  121. }
  122. }