Practice with string and loop

StringParser.java 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //Lauren
  2. import java.lang.*;
  3. /**
  4. * An introduction to Strings and String methods.
  5. *
  6. * @author Wilhem Alcivar
  7. */
  8. public class StringParser
  9. {
  10. /**
  11. * Takes a String and returns that String with all characters uppercased.
  12. * E.G. cat would become CAT. dOnUt would become DONUT.
  13. *
  14. * @param s
  15. * @return String
  16. */
  17. public static String upperCaseString(String s)
  18. {
  19. return s.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. return s.toLowerCase();
  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. return s.charAt(0);
  40. }
  41. /**
  42. * Takes a String and returns the character at index n of that string.
  43. * E.G. cat, 2 would return t. Embark, 4 would return r.
  44. *
  45. * @param s
  46. * @param n
  47. * @return String
  48. */
  49. public static Character getNthCharacter(String s, Integer n) {
  50. return s.charAt(n);
  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. return s.substring(0,1).toUpperCase() + s.substring(1, s.length());
  61. }
  62. /**
  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. s = s.toLowerCase();
  72. String split[] = s.split(" ");
  73. String result = "";
  74. for (int i = 0; i < split.length; i++) {
  75. split[i].trim();
  76. split[i] = split[i].substring(0,1).toUpperCase() + split[i].substring(1);
  77. result = result + split[i];
  78. }
  79. return result;
  80. }
  81. /**
  82. * Takes a String and returns that string with each character lowercased
  83. * and then joined with an underscore
  84. * E.G. dog whistle would return dog_whistle. adjuNCT pRoFessOR would return adjuct_professor.
  85. *
  86. * @param s
  87. * @return String
  88. */
  89. public static String snakeCaseString(String s) {
  90. return s.toLowerCase().replace(" ", "_");
  91. }
  92. /**
  93. * Takes a String and returns the length of that string
  94. * E.G. cat would return 3. extra would return 5.
  95. *
  96. * @param s
  97. * @return String
  98. */
  99. public static Integer getLength(String s) {
  100. return s.length();
  101. }
  102. /**
  103. * Takes in two strings and returns true if they are equal
  104. * E.G. example and shelf would return false.
  105. * cat and CaT would return false.
  106. * Dog and Dog would return true
  107. *
  108. * @param s1
  109. * @param s2
  110. * @return String
  111. */
  112. public static Boolean isEqual(String s1, String s2) {
  113. if (s1.compareTo(s2) == 0) {
  114. return true;
  115. } else {
  116. return false;
  117. }
  118. }
  119. /**
  120. * Takes in two strings and returns true if they are equal
  121. * E.G. example and shelf would return false.
  122. * cat and CaT would return true.
  123. * Dog and Dog would return true
  124. *
  125. * @param s1
  126. * @param s2
  127. * @return String
  128. */
  129. public static Boolean isEqualIgnoreCase(String s1, String s2) {
  130. if (s1.compareToIgnoreCase(s2) == 0) {
  131. return true;
  132. } else {
  133. return false;
  134. }
  135. }
  136. }