Practice with string and loop

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /**
  2. * An introduction to Strings and String methods.
  3. *
  4. * @author Wilhem Alcivar
  5. */
  6. //ElliottStansbury
  7. public class StringParser
  8. {
  9. /**
  10. * Takes a String and returns that String with all characters uppercased.
  11. * E.G. cat would become CAT. dOnUt would become DONUT.
  12. *
  13. * @param s
  14. * @return String
  15. */
  16. public static String upperCaseString(String s)
  17. {
  18. return s.toUpperCase();
  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. return s.toLowerCase();
  29. }
  30. /**
  31. * Takes a String and returns the first character of that string.
  32. * E.G. cat would return c. Embark would return E.
  33. *
  34. * @param s
  35. * @return String
  36. */
  37. public static Character getFirstCharacter(String s) {
  38. return s.charAt(0);
  39. }
  40. /**
  41. * Takes a String and returns the character at index n of that string.
  42. * E.G. cat, 2 would return t. Embark, 4 would return r.
  43. *
  44. * @param s
  45. * @param n
  46. * @return String
  47. */
  48. public static Character getNthCharacter(String s, Integer n) {
  49. char nLetter = s.charAt(n);
  50. return nLetter;
  51. }
  52. /**
  53. * Takes a String and returns that string with the first character uppercased.
  54. * E.G. cat would return Cat. cofFee would return CofFee.
  55. *
  56. * @param s
  57. * @return String
  58. */
  59. public static String upperCaseFirstCharacter(String s) {
  60. String firstLetterUpper = s.substring(0,1).toUpperCase() + s.substring(1);
  61. return firstLetterUpper;
  62. }
  63. /**
  64. * Takes a String and returns that string with the first character of each word in it uppercased
  65. * and then joined.
  66. * E.G. dog whistle would return DogWhistle. adjuNCT pRoFessOR would return AdjuctProfessor.
  67. *
  68. * @param s
  69. * @return String
  70. */
  71. public static String camelCaseString(String s) {
  72. String st = s.toLowerCase();
  73. String[] arr = st.split(" ");
  74. StringBuffer sb = new StringBuffer();
  75. for (int i = 0; i < arr.length; i++) {
  76. sb.append(Character.toUpperCase(arr[i].charAt(0)))
  77. .append(arr[i].substring(1));
  78. }
  79. String a = sb.toString();
  80. return a;
  81. }
  82. /**
  83. * Takes a String and returns that string with each character lowercased
  84. * and then joined with an underscore
  85. * E.G. dog whistle would return dog_whistle. adjuNCT pRoFessOR would return adjuct_professor.
  86. *
  87. * @param s
  88. * @return String
  89. */
  90. public static String snakeCaseString(String s) {
  91. String lower = s.toLowerCase();
  92. String a = lower.replaceAll("\\s","_");
  93. return a;
  94. }
  95. /**
  96. * Takes a String and returns the length of that string
  97. * E.G. cat would return 3. extra would return 5.
  98. *
  99. * @param s
  100. * @return String
  101. */
  102. public static Integer getLength(String s) {
  103. return s.length();
  104. }
  105. /**
  106. * Takes in two strings and returns true if they are equal
  107. * E.G. example and shelf would return false.
  108. * cat and CaT would return false.
  109. * Dog and Dog would return true
  110. *
  111. * @param s1
  112. * @param s2
  113. * @return String
  114. */
  115. public static Boolean isEqual(String s1, String s2) {
  116. if(s1.equals(s2)){
  117. return true;
  118. }else{
  119. }
  120. return false;
  121. }
  122. /**
  123. * Takes in two strings and returns true if they are equal
  124. * E.G. example and shelf would return false.
  125. * cat and CaT would return true.
  126. * Dog and Dog would return true
  127. *
  128. * @param s1
  129. * @param s2
  130. * @return String
  131. */
  132. public static Boolean isEqualIgnoreCase(String s1, String s2) {
  133. if(s1.equalsIgnoreCase(s2)){
  134. return true;
  135. }else {
  136. }
  137. return false;
  138. }
  139. }