Practice with string and loop

StringParser.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. char c = s.charAt(n);
  49. return c;
  50. }
  51. /**
  52. * Takes a String and returns that string with the first character uppercased.
  53. * E.G. cat would return Cat. cofFee would return CofFee.
  54. *
  55. * @param s
  56. * @return String
  57. */
  58. public static String upperCaseFirstCharacter(String s) {
  59. // substring has a beginning index and end index
  60. // if one doesn't put the end index it'll return the rest of the words
  61. // after the beginning index. Else it'll just return 1 character in thid
  62. // exercise
  63. String c = s.substring(0,1).toUpperCase() + s.substring(1);
  64. return c;
  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 c = s.toLowerCase();
  76. String x1 = c.split(" ")[0];
  77. String x2 = c.split(" ")[1];
  78. String container = x1.substring(0,1).toUpperCase() + x1.substring(1);
  79. String container1 = x2.substring(0,1).toUpperCase() + x2.substring(1);
  80. String putittogether = container + container1;
  81. return putittogether.trim();
  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 c = s.replace(" ","_");
  93. return c.toLowerCase();
  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. int c = s.length();
  104. return c;
  105. }
  106. /**
  107. * Takes in two strings and returns true if they are equal
  108. * E.G. example and shelf would return false.
  109. * cat and CaT would return false.
  110. * Dog and Dog would return true
  111. *
  112. * @param s1
  113. * @param s2
  114. * @return String
  115. */
  116. public static Boolean isEqual(String s1, String s2) {
  117. if (s1.equals(s2)){
  118. return true;
  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. }
  136. return false;
  137. }
  138. }