Practice with string and loop

StringParser.java 4.3KB

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. char firstLet= s.charAt(0);
  38. return firstLet;
  39. }
  40. /**
  41. * Takes a String and returns the character at index n of that string.
  42. * E.G. cat, 2 would return t. Embark, 4 would return r.
  43. *
  44. * @param s
  45. * @param n
  46. * @return String
  47. */
  48. public static Character getNthCharacter(String s, Integer n) {
  49. char val=s.charAt(n);
  50. return val;
  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. String cap = s.substring(0, 1).toUpperCase() + s.substring(1);
  61. return cap;
  62. }
  63. /**
  64. * Takes a String and returns that string with the first character of each word in it uppercased
  65. * and then joined.
  66. * E.G. dog whistle would return DogWhistle. adjuNCT pRoFessOR would return AdjuctProfessor.
  67. *
  68. * @param s
  69. * @return String
  70. */
  71. public static String camelCaseString(String s){
  72. String sum=s.toLowerCase();
  73. String part1= sum.split(" ")[0];
  74. String part2= sum.split(" ")[1];
  75. String firstHalf= part1.substring(0, 1).toUpperCase() + part1.substring(1).toLowerCase();
  76. String secondHalf= part2.substring(0, 1).toUpperCase() + part2.substring(1).toLowerCase();
  77. String theSum=firstHalf+secondHalf;
  78. return theSum;
  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. int n = s.length();
  90. String str1 = "";
  91. for (int i = 0; i < n; i++)
  92. {
  93. // Converting space
  94. // to underscor
  95. if (s.charAt(i) == ' ')
  96. str1 = str1 + '_';
  97. else
  98. // If not space, convert
  99. // into lower character
  100. str1 = str1 +
  101. Character.toLowerCase(s.charAt(i));
  102. }
  103. return str1;
  104. }
  105. /**
  106. * Takes a String and returns the length of that string
  107. * E.G. cat would return 3. extra would return 5.
  108. *
  109. * @param s
  110. * @return String
  111. */
  112. public static Integer getLength(String s) {
  113. return s.length();
  114. }
  115. /**
  116. * Takes in two strings and returns true if they are equal
  117. * E.G. example and shelf would return false.
  118. * cat and CaT would return false.
  119. * Dog and Dog would return true
  120. *
  121. * @param s1
  122. * @param s2
  123. * @return String
  124. */
  125. public static Boolean isEqual(String s1, String s2) {
  126. if(s1.equals(s2)){
  127. return true;
  128. } else {
  129. return false;
  130. }
  131. }
  132. /**
  133. * Takes in two strings and returns true if they are equal
  134. * E.G. example and shelf would return false.
  135. * cat and CaT would return true.
  136. * Dog and Dog would return true
  137. *
  138. * @param s1
  139. * @param s2
  140. * @return String
  141. */
  142. public static Boolean isEqualIgnoreCase(String s1, String s2) {
  143. if(s1.equalsIgnoreCase(s2)){
  144. return true;
  145. } else {
  146. return false;
  147. }
  148. }
  149. }