Practice with string and loop

StringParser.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /**
  2. * An introduction to Strings and String methods.
  3. *
  4. * @author Wilhem Alcivar
  5. */
  6. public class StringParser
  7. {
  8. private static String s1;
  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. return 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. return s.toLowerCase();
  29. }
  30. /**
  31. * Takes a String and returns the first character of that string.
  32. * E.G. cat would return c. Embark would return E.
  33. *
  34. * @param s
  35. * @return String
  36. */
  37. public static Character getFirstCharacter(String s) {
  38. return s.charAt(0);
  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. return s.charAt(n);
  50. }
  51. /**
  52. * Takes a String and returns that string with the first character uppercased.
  53. * E.G. cat would return Cat. cofFee would return CofFee.
  54. *
  55. * @param s
  56. * @return String
  57. */
  58. public static String upperCaseFirstCharacter(String s) {
  59. s1 = s.substring(0,1).toUpperCase() + s.substring(1);
  60. return s1;
  61. }
  62. /**
  63. * Takes a String and returns that string with the first character of each word in it uppercased
  64. * and then joined.
  65. * E.G. dog whistle would return DogWhistle. adjuNCT pRoFessOR would return AdjuctProfessor.
  66. *
  67. * @param s
  68. * @return String
  69. */
  70. public static String camelCaseString(String s) {
  71. //String s2 = "";
  72. s1 = s.toLowerCase();
  73. //s1 = s1.subString(0,1) + s1.subString(1);
  74. //StringBuffer bf = new StringBuffer(s1);
  75. for(int i=0;i<s1.length();i++)
  76. {
  77. if(s1.charAt(i) == ' '){
  78. s=s1.substring(0,1).toUpperCase()+s1.substring(1,i);
  79. s1=s1.substring(i+1,i+2).toUpperCase()+s1.substring(i+2);
  80. return s+s1;
  81. }
  82. else{
  83. s=s1.substring(0,1).toUpperCase()+s1.substring(1);
  84. }
  85. }
  86. return null;
  87. }
  88. /**
  89. * Takes a String and returns that string with each character lowercased
  90. * and then joined with an underscore
  91. * E.G. dog whistle would return dog_whistle. adjuNCT pRoFessOR would return adjuct_professor.
  92. *
  93. * @param s
  94. * @return String
  95. */
  96. public static String snakeCaseString(String s) {
  97. StringBuffer bf = new StringBuffer(s.toLowerCase());
  98. for(int i=0;i<bf.length();i++)
  99. {
  100. if(bf.charAt(i) == ' ')
  101. {
  102. bf.insert(i,"_");
  103. bf.deleteCharAt(i+1);
  104. s= bf.toString();
  105. return s;
  106. }
  107. }
  108. return s;
  109. }
  110. /**
  111. * Takes a String and returns the length of that string
  112. * E.G. cat would return 3. extra would return 5.
  113. *
  114. * @param s
  115. * @return String
  116. */
  117. public static Integer getLength(String s) {
  118. return s.length();
  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 false.
  124. * Dog and Dog would return true
  125. *
  126. * @param s1
  127. * @param s2
  128. * @return String
  129. */
  130. public static Boolean isEqual(String s1, String s2) {
  131. return s1.equals(s2);
  132. }
  133. /**
  134. * Takes in two strings and returns true if they are equal
  135. * E.G. example and shelf would return false.
  136. * cat and CaT would return true.
  137. * Dog and Dog would return true
  138. *
  139. * @param s1
  140. * @param s2
  141. * @return String
  142. */
  143. public static Boolean isEqualIgnoreCase(String s1, String s2) {
  144. return s1.equalsIgnoreCase(s2);
  145. }
  146. }