Practice with string and loop

StringParser.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. String str = s;
  59. String cap = str.substring(0,1).toUpperCase() + str.substring(1);
  60. return cap;
  61. }
  62. /** NEED TO FINISH
  63. * Takes a String and returns that string with the first character of each word in it uppercased
  64. * and then joined.
  65. * E.G. dog whistle would return DogWhistle. adjuNCT pRoFessOR would return AdjuctProfessor.
  66. *
  67. * @param s
  68. * @return String
  69. */
  70. public static String camelCaseString(String s) {
  71. String lower = s.toLowerCase();
  72. String first = " ";
  73. for (int i = 0; i < lower.length(); i++) {
  74. if (Character.isWhitespace(lower.charAt(i))) {
  75. first = lower.substring(0,i);
  76. break;
  77. }
  78. }
  79. first = first.substring(0,1).toUpperCase() + first.substring(1);
  80. String second = lower.substring(first.length()+1);
  81. second = second.substring(0,1).toUpperCase() + second.substring(1);
  82. String cC = first + second;
  83. return cC;
  84. }
  85. /**
  86. * Takes a String and returns that string with each character lowercased
  87. * and then joined with an underscore
  88. * E.G. dog whistle would return dog_whistle. adjuNCT pRoFessOR would return adjuct_professor.
  89. *
  90. * @param s
  91. * @return String
  92. */
  93. public static String snakeCaseString(String s) {
  94. String itemname = s;
  95. itemname = itemname.replaceAll(" ","_").toLowerCase();
  96. String finale = itemname;
  97. System.out.print(finale);
  98. return finale;
  99. }
  100. /**
  101. * Takes a String and returns the length of that string
  102. * E.G. cat would return 3. extra would return 5.
  103. *
  104. * @param s
  105. * @return String
  106. */
  107. public static Integer getLength(String s) {
  108. return s.length();
  109. }
  110. /**
  111. * Takes in two strings and returns true if they are equal
  112. * E.G. example and shelf would return false.
  113. * cat and CaT would return false.
  114. * Dog and Dog would return true
  115. *
  116. * @param s1
  117. * @param s2
  118. * @return String
  119. */
  120. public static Boolean isEqual(String s1, String s2) {
  121. if (s1 == s2) {
  122. return true;
  123. } else {
  124. return false;
  125. }
  126. }
  127. /**
  128. * Takes in two strings and returns true if they are equal
  129. * E.G. example and shelf would return false.
  130. * cat and CaT would return true.
  131. * Dog and Dog would return true
  132. *
  133. * @param s1
  134. * @param s2
  135. * @return String
  136. */
  137. public static Boolean isEqualIgnoreCase(String s1, String s2) {
  138. if (s1.equalsIgnoreCase(s2)) {
  139. return true;
  140. } else {
  141. return false;
  142. }
  143. }
  144. }