Practice with string and loop

StringParser.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. String convert = s.substring(0,1);
  38. char solution = convert.charAt(0);
  39. return solution;
  40. }
  41. /**
  42. * Takes a String and returns the character at index n of that string.
  43. * E.G. cat, 2 would return t. Embark, 4 would return r.
  44. *
  45. * @param s
  46. * @param n
  47. * @return String
  48. */
  49. public static Character getNthCharacter(String s, Integer n) {
  50. int firstnum = n + 1;
  51. String thechar = s.substring(n,firstnum);
  52. char itsthechar = thechar.charAt(0);
  53. return itsthechar;
  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. int length = s.length();
  64. String upperchar = s.substring(0,1).toUpperCase();
  65. return upperchar + s.substring(1,length);
  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. String[] splitem = s.split("\\s+");
  77. String upperchar1 = splitem[0].substring(0,1).toUpperCase();
  78. String upperchar2 = splitem[1].substring(0,1).toUpperCase();
  79. String lowerchar1 = splitem[0].substring(1,splitem[0].length()).toLowerCase();
  80. String lowerchar2 = splitem[1].substring(1,splitem[1].length()).toLowerCase();
  81. return upperchar1 + lowerchar1 + upperchar2 + lowerchar2;
  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[] splitem = s.split("\\s+");
  93. String first = splitem[0].toLowerCase();
  94. String second = splitem[1].toLowerCase();
  95. return first + "_" + second;
  96. }
  97. /**
  98. * Takes a String and returns the length of that string
  99. * E.G. cat would return 3. extra would return 5.
  100. *
  101. * @param s
  102. * @return String
  103. */
  104. public static Integer getLength(String s) {
  105. return s.length();
  106. }
  107. /**
  108. * Takes in two strings and returns true if they are equal
  109. * E.G. example and shelf would return false.
  110. * cat and CaT would return false.
  111. * Dog and Dog would return true
  112. *
  113. * @param s1
  114. * @param s2
  115. * @return String
  116. */
  117. public static Boolean isEqual(String s1, String s2) {
  118. if (s1 == s2) {return true;} else {return false;}
  119. }
  120. /**
  121. * Takes in two strings and returns true if they are equal
  122. * E.G. example and shelf would return false.
  123. * cat and CaT would return true.
  124. * Dog and Dog would return true
  125. *
  126. * @param s1
  127. * @param s2
  128. * @return String
  129. */
  130. public static Boolean isEqualIgnoreCase(String s1, String s2) {
  131. return s1.equals(s2);
  132. }
  133. }