Practice with string and loop

StringParser.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package JavaStringCheese;
  2. /**
  3. * An introduction to Strings and String methods.
  4. *
  5. * @author Wilhem Alcivar
  6. */
  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. String upperCase = s;
  19. return upperCase.toUpperCase();
  20. }
  21. /**
  22. * Takes a String and returns that String with all characters lowercased.
  23. * E.G. MOUSE would become mouse. dOnUt would become donut.
  24. *
  25. * @param s
  26. * @return String
  27. */
  28. public static String lowerCaseString(String s) {
  29. String lowerCase = s;
  30. return lowerCase.toLowerCase();
  31. }
  32. /**
  33. * Takes a String and returns the first character of that string.
  34. * E.G. cat would return c. Embark would return E.
  35. *
  36. * @param s
  37. * @return String
  38. */
  39. public static Character getFirstCharacter(String s) {
  40. String char1 = s;
  41. return char1.charAt(0);
  42. }
  43. /**
  44. * Takes a String and returns the character at index n of that string.
  45. * E.G. cat, 2 would return t. Embark, 4 would return r.
  46. *
  47. * @param s
  48. * @param n
  49. * @return String
  50. */
  51. public static Character getNthCharacter(String s, Integer n) {
  52. String charN = s;
  53. return charN.charAt(n);
  54. }
  55. /**
  56. * Takes a String and returns that string with the first character uppercased.
  57. * E.G. cat would return Cat. cofFee would return CofFee.
  58. *
  59. * @param s
  60. * @return String
  61. */
  62. public static String upperCaseFirstCharacter(String s) {
  63. String string1 = s;
  64. return string1.substring(0,1).toUpperCase() + string1.substring(1);
  65. }
  66. /**
  67. * Takes a String and returns that string with the first character of each word in it uppercased
  68. * and then joined.
  69. * E.G. dog whistle would return DogWhistle. adjuNCT pRoFessOR would return AdjuctProfessor.
  70. *
  71. * @param s
  72. * @return String
  73. */
  74. public static String camelCaseString(String s) {
  75. String string1 = s;
  76. int space = string1.indexOf(" ");
  77. String string2 = string1.substring(0,1).toUpperCase();
  78. String string3 = string1.substring(1, space).toLowerCase();
  79. String string4 = string1.substring(space+1, space+2).toUpperCase();
  80. String string5 = string1.substring(space+2).toLowerCase();
  81. return string2 + string3 + string4 + string5;
  82. }
  83. /**
  84. * Takes a String and returns that string with each character lowercased
  85. * and then joined with an underscore
  86. * E.G. dog whistle would return dog_whistle. adjuNCT pRoFessOR would return adjuct_professor.
  87. *
  88. * @param s
  89. * @return String
  90. */
  91. public static String snakeCaseString(String s) {
  92. String string1 = s;
  93. int space = string1.indexOf(" ");
  94. String string2 = string1.substring(0,space).toLowerCase();
  95. String string3 = string1.substring(space, space+1);
  96. String string4 = "_";
  97. String string5 = string1.substring(space+1).toLowerCase();
  98. return string2 + string4 + string5;
  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. String string1 = s;
  109. return s.length();
  110. }
  111. /**
  112. * Takes in two strings and returns true if they are equal
  113. * E.G. example and shelf would return false.
  114. * cat and CaT would return false.
  115. * Dog and Dog would return true
  116. *
  117. * @param s1
  118. * @param s2
  119. * @return String
  120. */
  121. public static Boolean isEqual(String s1, String s2) {
  122. String string1 = s1;
  123. String string2 = s2;
  124. return string1.equals(string2);
  125. }
  126. /**
  127. * Takes in two strings and returns true if they are equal
  128. * E.G. example and shelf would return false.
  129. * cat and CaT would return true.
  130. * Dog and Dog would return true
  131. *
  132. * @param s1
  133. * @param s2
  134. * @return String
  135. */
  136. public static Boolean isEqualIgnoreCase(String s1, String s2) {
  137. String string1 = s1;
  138. String string2 = s2;
  139. return string1.equalsIgnoreCase(string2);
  140. }
  141. }