Practice with string and loop

StringParser.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 upperC = new String();
  18. return upperC = 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. String lowerC = new String();
  29. return lowerC= 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 firstLetter = s.charAt(0);
  40. return firstLetter;
  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 nthChar = s.charAt(n);
  52. return nthChar;
  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 upperCase = new String();
  63. String restWord = new String();
  64. upperCase = s.substring(0,1).toUpperCase();
  65. restWord = s.substring(1);
  66. return upperCase + restWord;
  67. }
  68. /**
  69. * Takes a String and returns that string with the first character of each word in it uppercased
  70. * and then joined.
  71. * E.G. dog whistle would return DogWhistle. adjuNCT pRoFessOR would return AdjuctProfessor.
  72. *
  73. * @param s
  74. * @return String
  75. */
  76. public static String camelCaseString(String s) {
  77. s = s.toLowerCase();
  78. String camelCase = new String();
  79. int secWordIndex = s.indexOf(" ")+1;
  80. camelCase = s.substring(0,1).toUpperCase() + s.substring(1, secWordIndex) +
  81. s.substring(secWordIndex,secWordIndex+1).toUpperCase() + s.substring(secWordIndex+1);
  82. camelCase = camelCase.replace(" ","");
  83. return camelCase;
  84. }
  85. /**
  86. * Takes a String and returns that string with each character lowercased
  87. * and then joined with an underscore
  88. * E.G. dog whistle would return dog_whistle. adjuNCT pRoFessOR would return adjuct_professor.
  89. *
  90. * @param s
  91. * @return String
  92. */
  93. public static String snakeCaseString(String s) {
  94. String snakes = new String();
  95. snakes = s.toLowerCase().replace(" ", "_");
  96. return snakes;
  97. }
  98. /**
  99. * Takes a String and returns the length of that string
  100. * E.G. cat would return 3. extra would return 5.
  101. *
  102. * @param s
  103. * @return String
  104. */
  105. public static Integer getLength(String s) {
  106. return s.length();
  107. }
  108. /**
  109. * Takes in two strings and returns true if they are equal
  110. * E.G. example and shelf would return false.
  111. * cat and CaT would return false.
  112. * Dog and Dog would return true
  113. *
  114. * @param s1
  115. * @param s2
  116. * @return String
  117. */
  118. public static Boolean isEqual(String s1, String s2) {
  119. boolean result = s1.equals(s2);
  120. return result;
  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. s1 = s1.toLowerCase();
  134. s2 = s2.toLowerCase();
  135. return isEqual(s1,s2);
  136. }
  137. }