Practice with string and loop

StringParser.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. String uppercase = s.toUpperCase();
  18. return uppercase;
  19. }
  20. /**
  21. * Takes a String and returns that String with all characters lowercased.
  22. * E.G. MOUSE would become mouse. dOnUt would become donut.
  23. *
  24. * @param s
  25. * @return String
  26. */
  27. public static String lowerCaseString(String s) {
  28. String lowercase = s.toLowerCase();
  29. return lowercase;
  30. }
  31. /**
  32. * Takes a String and returns the first character of that string.
  33. * E.G. cat would return c. Embark would return E.
  34. *
  35. * @param s
  36. * @return String
  37. */
  38. public static Character getFirstCharacter(String s) {
  39. char firstChar = s.charAt(0);
  40. return firstChar;
  41. }
  42. /**
  43. * Takes a String and returns the character at index n of that string.
  44. * E.G. cat, 2 would return t. Embark, 4 would return r.
  45. *
  46. * @param s
  47. * @param n
  48. * @return String
  49. */
  50. public static Character getNthCharacter(String s, Integer n) {
  51. char nthChar = s.charAt(n);
  52. return nthChar;
  53. }
  54. /**
  55. * Takes a String and returns that string with the first character uppercased.
  56. * E.G. cat would return Cat. cofFee would return CofFee.
  57. *
  58. * @param s
  59. * @return String
  60. */
  61. public static String upperCaseFirstCharacter(String s) {
  62. String capitalized = s.substring(0, 1).toUpperCase() + s.substring(1);
  63. return capitalized;
  64. }
  65. /**
  66. * Takes a String and returns that string with the first character of each word in it uppercased
  67. * and then joined.
  68. * E.G. dog whistle would return DogWhistle. adjuNCT pRoFessOR would return AdjuctProfessor.
  69. *
  70. * @param s
  71. * @return String
  72. */
  73. public static String camelCaseString(String s) {
  74. StringBuilder camelCaseSB = new StringBuilder();
  75. String capitalFirst = s.substring(0, 1).toUpperCase();
  76. camelCaseSB.append(capitalFirst);
  77. for (int i = 1; i < s.length(); i++) {
  78. if (s.charAt(i) == ' ') {
  79. String nextCapital = s.substring(i + 1, i+ 2).toUpperCase();
  80. camelCaseSB.append(nextCapital);
  81. i++;
  82. } else {
  83. camelCaseSB.append(s.substring(i, i + 1).toLowerCase());
  84. }
  85. }
  86. /* int firstSpace = s.indexOf(" ");
  87. String capitalSecond = s.substring(firstSpace + 1, firstSpace + 2).toUpperCase() + s.substring(1);
  88. String camelCaseString = capitalFirst.concat(capitalSecond);
  89. int secondSpace = s.indexOf(" ", firstSpace + 1);
  90. String secondWord;
  91. if (secondSpace > 0) {
  92. secondWord = s.substring(firstSpace, secondSpace);
  93. } else {
  94. secondWord = s.substring((firstSpace + 1),(s.length()));
  95. } */
  96. String camelCase = camelCaseSB.toString();
  97. return camelCase;
  98. }
  99. /**
  100. * Takes a String and returns that string with each character lowercased
  101. * and then joined with an underscore
  102. * E.G. dog whistle would return dog_whistle. adjuNCT pRoFessOR would return adjuct_professor.
  103. *
  104. * @param s
  105. * @return String
  106. */
  107. public static String snakeCaseString(String s) {
  108. StringBuilder snakeCaseSB = new StringBuilder();
  109. // String capitalFirst = s.substring(0, 1).toUpperCase();
  110. // snakeCaseSB.append(capitalFirst);
  111. for (int i = 0; i < s.length(); i++) {
  112. if (s.charAt(i) == ' ') {
  113. snakeCaseSB.append('_');
  114. } else {
  115. snakeCaseSB.append(s.substring(i, i + 1).toLowerCase());
  116. }
  117. }
  118. String snakeCase = snakeCaseSB.toString();
  119. return snakeCase;
  120. }
  121. /**
  122. * Takes a String and returns the length of that string
  123. * E.G. cat would return 3. extra would return 5.
  124. *
  125. * @param s
  126. * @return String
  127. */
  128. public static Integer getLength(String s) {
  129. return s.length();
  130. }
  131. /**
  132. * Takes in two strings and returns true if they are equal
  133. * E.G. example and shelf would return false.
  134. * cat and CaT would return false.
  135. * Dog and Dog would return true
  136. *
  137. * @param s1
  138. * @param s2
  139. * @return String
  140. */
  141. public static Boolean isEqual(String s1, String s2) {
  142. boolean s3;
  143. if (s1.equals(s2)) {
  144. s3 = true;
  145. } else {
  146. s3 = false;
  147. }
  148. return s3;
  149. }
  150. /**
  151. * Takes in two strings and returns true if they are equal
  152. * E.G. example and shelf would return false.
  153. * cat and CaT would return true.
  154. * Dog and Dog would return true
  155. *
  156. * @param s1
  157. * @param s2
  158. * @return String
  159. */
  160. public static Boolean isEqualIgnoreCase(String s1, String s2) {
  161. boolean s3;
  162. if (s1.equalsIgnoreCase(s2)) {
  163. s3 = true;
  164. } else {
  165. s3 = false;
  166. }
  167. return s3;
  168. }
  169. }