Practice with string and loop

StringParser.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. String str;
  18. return str= s.toUpperCase();
  19. }
  20. /**
  21. * Takes a String and returns that String with all characters lowercased.
  22. * E.G. MOUSE would become mouse. dOnUt would become donut.
  23. *
  24. * @param s
  25. * @return String
  26. */
  27. public static String lowerCaseString(String s)
  28. {
  29. String str;
  30. return str = s.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. {
  41. return s.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. {
  53. return s.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. {
  64. String str = s.substring(0,1).toUpperCase()+s.substring(1);
  65. return str;
  66. }
  67. /**
  68. * Takes a String and returns that string with the first character of each word in it uppercased
  69. * and then joined.
  70. * E.G. dog whistle would return DogWhistle. adjuNCT pRoFessOR would return AdjuctProfessor.
  71. *
  72. * @param s
  73. * @return String
  74. */
  75. public static String camelCaseString(String s)
  76. {
  77. String [] list = s.split(" ");
  78. String str1 = list[0];
  79. String str2 = list[1];
  80. return (""+str1.toUpperCase().charAt(0)+str1.toLowerCase().substring(1,str1.length())+str2.toUpperCase().charAt(0)+str2.toLowerCase().substring(1,str2.length()));
  81. }
  82. /**
  83. * Takes a String and returns that string with each character lowercased
  84. * and then joined with an underscore
  85. * E.G. dog whistle would return dog_whistle. adjuNCT pRoFessOR would return adjuct_professor.
  86. *
  87. * @param s
  88. * @return String
  89. */
  90. public static String snakeCaseString(String s)
  91. {
  92. int n = s.length();
  93. String str = "";
  94. for(int i = 0; i < n; i++)
  95. {
  96. if(s.charAt(i) == ' ')
  97. str = str + '_';
  98. else
  99. str = str + Character.toLowerCase(s.charAt(i));
  100. }
  101. return str;
  102. }
  103. /**
  104. * Takes a String and returns the length of that string
  105. * E.G. cat would return 3. extra would return 5.
  106. *
  107. * @param s
  108. * @return String
  109. */
  110. public static Integer getLength(String s)
  111. {
  112. return s.length();
  113. }
  114. /**
  115. * Takes in two strings and returns true if they are equal
  116. * E.G. example and shelf would return false.
  117. * cat and CaT would return false.
  118. * Dog and Dog would return true
  119. *
  120. * @param s1
  121. * @param s2
  122. * @return String
  123. */
  124. public static Boolean isEqual(String s1, String s2)
  125. {
  126. if(s1.equals(s2))
  127. {
  128. return true;
  129. }
  130. else
  131. {
  132. return false;
  133. }
  134. }
  135. /**
  136. * Takes in two strings and returns true if they are equal
  137. * E.G. example and shelf would return false.
  138. * cat and CaT would return true.
  139. * Dog and Dog would return true
  140. *
  141. * @param s1
  142. * @param s2
  143. * @return String
  144. */
  145. public static Boolean isEqualIgnoreCase(String s1, String s2)
  146. {
  147. return (s1.equalsIgnoreCase(s2));
  148. }
  149. }