Practice with string and loop

StringParser.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /**
  2. * An introduction to Strings and String methods.
  3. *
  4. * @author Wilhem Alcivar
  5. */
  6. import java.util.*;
  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 change = s.toUpperCase();
  19. return change;
  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. char first = s.charAt(0);
  40. return first;
  41. }
  42. /**
  43. * Takes a String and returns the character at index n of that string.
  44. * E.G. cat, 2 would return t. Embark, 4 would return r.
  45. *
  46. * @param s
  47. * @param n
  48. * @return String
  49. */
  50. public static Character getNthCharacter(String s, Integer n) {
  51. char num = s.charAt(n);
  52. return num;
  53. }
  54. /**
  55. * Takes a String and returns that string with the first character uppercased.
  56. * E.G. cat would return Cat. cofFee would return CofFee.
  57. *
  58. * @param s
  59. * @return String
  60. */
  61. public static String upperCaseFirstCharacter(String s) {
  62. String change = s.substring(0,1).toUpperCase();
  63. String nameCap = change + s.substring(1);
  64. return nameCap;
  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. s = s.toLowerCase();
  76. int index = s.indexOf(" ") + 1;
  77. s = s.substring(0,1).toUpperCase() + s.substring(1,index -1)+ s.substring(index, index +1).toUpperCase() + s.substring(index +1);
  78. return s;
  79. }
  80. /**
  81. * Takes a String and returns that string with each character lowercased
  82. * and then joined with an underscore
  83. * E.G. dog whistle would return dog_whistle. adjuNCT pRoFessOR would return adjuct_professor.
  84. *
  85. * @param s
  86. * @return String
  87. */
  88. public static String snakeCaseString(String s) {
  89. String val = s.toLowerCase();
  90. val = val.replaceAll(" ","_");
  91. return val;
  92. }
  93. /**
  94. * Takes a String and returns the length of that string
  95. * E.G. cat would return 3. extra would return 5.
  96. *
  97. * @param s
  98. * @return String
  99. */
  100. public static Integer getLength(String s) {
  101. int quantity = s.length();
  102. return quantity;
  103. }
  104. /**
  105. * Takes in two strings and returns true if they are equal
  106. * E.G. example and shelf would return false.
  107. * cat and CaT would return false.
  108. * Dog and Dog would return true
  109. *
  110. * @param s1
  111. * @param s2
  112. * @return String
  113. */
  114. public static Boolean isEqual(String s1, String s2) {
  115. if(s1.equals(s2))
  116. {
  117. return true;
  118. }
  119. return false;
  120. }
  121. /**
  122. * Takes in two strings and returns true if they are equal
  123. * E.G. example and shelf would return false.
  124. * cat and CaT would return true.
  125. * Dog and Dog would return true
  126. *
  127. * @param s1
  128. * @param s2
  129. * @return String
  130. */
  131. public static Boolean isEqualIgnoreCase(String s1, String s2) {
  132. if(s1.equalsIgnoreCase(s2))
  133. {
  134. return true;
  135. }
  136. return false;
  137. }
  138. }