Practice with string and loop

StringParser.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /**
  2. * An introduction to Strings and String methods.
  3. *
  4. * @author Wilhem Alcivar
  5. */
  6. import java.util.*;
  7. public class StringParser
  8. {
  9. public static String upperCaseString(String s) {
  10. return s.toUpperCase();
  11. }
  12. /**
  13. * Takes a String and returns that String with all characters lowercased.
  14. * E.G. MOUSE would become mouse. dOnUt would become donut.
  15. *
  16. * @param s
  17. * @return String
  18. */
  19. public static String lowerCaseString(String s) {
  20. return s.toLowerCase();
  21. }
  22. /**
  23. * Takes a String and returns the first character of that string.
  24. * E.G. cat would return c. Embark would return E.
  25. *
  26. * @param s
  27. * @return String
  28. *
  29. */
  30. public static Character getFirstCharacter(String s) {
  31. return s.charAt(0);
  32. }
  33. /**
  34. * Takes a String and returns the character at index n of that string.
  35. * E.G. cat, 2 would return t. Embark, 4 would return r.
  36. *
  37. * @param s
  38. * @param n
  39. * @return String
  40. */
  41. public static Character getNthCharacter(String s, Integer n) {
  42. return s.charAt(n);
  43. }
  44. /**
  45. * Takes a String and returns that string with the first character uppercased.
  46. * E.G. cat would return Cat. cofFee would return CofFee.
  47. *
  48. * @param s
  49. * @return String
  50. */
  51. public static String upperCaseFirstCharacter(String s) {
  52. //t = Character.toUpperCase(t.charAt(0)) + t.substring(1);
  53. //System.out.println(t);
  54. return (s.substring (0,1)).toUpperCase() + s.substring(1);
  55. }
  56. /**
  57. * Takes a String and returns that string with the first character of each word in it uppercased
  58. * and then joined.
  59. * E.G. dog whistle would return DogWhistle. adjuNCT pRoFessOR would return AdjuctProfessor.
  60. *
  61. * @param s
  62. * @return String
  63. */
  64. public static String camelCaseString(String s) {
  65. String [] words = s.split(" ");
  66. String t = "";
  67. int i;
  68. //String firstWord;
  69. for ( i = 0; i < words.length; i ++) {
  70. t += words[i].substring (0,1).toUpperCase() + words[i].substring(1).toLowerCase() ;
  71. //t.join(firstWord);
  72. }
  73. //String nextWord = words[1].substring (0,1).toUpperCase() + words[1].substring(1).toLowerCase();
  74. //String nextWord2 = words[2].substring (0,1).toUpperCase() + words[2].substring(1).toLowerCase();
  75. return t;
  76. }
  77. /**
  78. * Takes a String and returns that string with each character lowercased
  79. * and then joined with an underscore
  80. * E.G. dog whistle would return dog_whistle. adjuNCT pRoFessOR would return adjuct_professor.
  81. *
  82. * @param s
  83. * @return String
  84. */
  85. public static String snakeCaseString(String s) {
  86. return s.replaceAll(" ", "_").toLowerCase();
  87. }
  88. /**
  89. * Takes a String and returns the length of that string
  90. * E.G. cat would return 3. extra would return 5.
  91. *
  92. * @param s
  93. * @return String
  94. */
  95. public static Integer getLength(String s) {
  96. return s.length();
  97. }
  98. /**
  99. * Takes in two strings and returns true if they are equal
  100. * E.G. example and shelf would return false.
  101. * cat and CaT would return false.
  102. * Dog and Dog would return true
  103. *
  104. * @param s1
  105. * @param s2
  106. * @return String
  107. */
  108. public static Boolean isEqual(String s1, String s2) {
  109. if (s1.equals(s2)) {
  110. return true;
  111. } else return false;
  112. }
  113. /**
  114. * Takes in two strings and returns true if they are equal
  115. * E.G. example and shelf would return false.
  116. * cat and CaT would return true.
  117. * Dog and Dog would return true
  118. *
  119. * @param s1
  120. * @param s2
  121. * @return String
  122. */
  123. public static Boolean isEqualIgnoreCase(String s1, String s2) {
  124. if (s1.equalsIgnoreCase(s2)){
  125. return true;
  126. }
  127. else return false;
  128. }
  129. }