Practice with string and loop

StringParser.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 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. return s.substring(0,1).toUpperCase()+s.substring(1);
  60. }
  61. /**
  62. * Takes a String and returns that string with the first character of each word in it uppercased
  63. * and then joined.
  64. * E.G. dog whistle would return DogWhistle. adjuNCT pRoFessOR would return AdjuctProfessor.
  65. *
  66. * @param s
  67. * @return String
  68. */
  69. public static String camelCaseString(String s) {
  70. int x = s.indexOf(' ');
  71. return s.substring(0,1).toUpperCase() + s.substring(1,x).toLowerCase() + s.substring(x+1,x+2).toUpperCase() + s.substring(x+2).toLowerCase();
  72. }
  73. /**
  74. * Takes a String and returns that string with each character lowercased
  75. * and then joined with an underscore
  76. * E.G. dog whistle would return dog_whistle. adjuNCT pRoFessOR would return adjuct_professor.
  77. *
  78. * @param s
  79. * @return String
  80. */
  81. public static String snakeCaseString(String s) {
  82. int x = s.indexOf(' ');
  83. return s.substring(0,x).toLowerCase() + "_" + s.substring(x+1).toLowerCase();
  84. }
  85. /**
  86. * Takes a String and returns the length of that string
  87. * E.G. cat would return 3. extra would return 5.
  88. *
  89. * @param s
  90. * @return String
  91. */
  92. public static Integer getLength(String s) {
  93. return s.length();
  94. }
  95. /**
  96. * Takes in two strings and returns true if they are equal
  97. * E.G. example and shelf would return false.
  98. * cat and CaT would return false.
  99. * Dog and Dog would return true
  100. *
  101. * @param s1
  102. * @param s2
  103. * @return String
  104. */
  105. public static Boolean isEqual(String s1, String s2) {
  106. return s1.equals(s2);
  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 true.
  112. * Dog and Dog would return true
  113. *
  114. * @param s1
  115. * @param s2
  116. * @return String
  117. */
  118. public static Boolean isEqualIgnoreCase(String s1, String s2) {
  119. boolean fai = s1.equalsIgnoreCase(s2);
  120. return fai;
  121. }
  122. }