implement a whole bunch of simple methods.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /**
  2. * Created by dan on 6/14/17.
  3. */
  4. public class StringUtilities {
  5. /**
  6. * @return `Hello World` as a string
  7. */
  8. public String getHelloWorld() {
  9. String getHelloWorld = "Hello World";
  10. return getHelloWorld;
  11. }
  12. /**
  13. * @param firstSegment a string to be added to
  14. * @param secondSegment a string to add
  15. * @return the concatenation of two strings, `firstSegment`, and `secondSegment`
  16. */
  17. public String concatenation(String firstSegment, String secondSegment){
  18. String firstAndSecond = firstSegment + secondSegment;
  19. return firstAndSecond;
  20. }
  21. /**
  22. * @param firstSegment a string to be added to
  23. * @param secondSegment a string to add
  24. * @return the concatenation of an integer, `firstSegment`, and a String, `secondSegment`
  25. */
  26. public String concatenation(int firstSegment, String secondSegment){
  27. String newString = firstSegment + secondSegment;
  28. return newString;
  29. }
  30. /**
  31. * @param input a string to be manipulated
  32. * @return the first 3 characters of `input`
  33. */
  34. public String getPrefix(String input){
  35. String firstThree = input.substring(0, 3);
  36. return firstThree;
  37. }
  38. /**
  39. * @param input a string to be manipulated
  40. * @return the last 3 characters of `input`
  41. */
  42. public String getSuffix(String input){
  43. String lastThree = input.substring(input.length() - 3, input.length());
  44. return lastThree;
  45. }
  46. /**
  47. * @param inputValue the value to be compared
  48. * @param comparableValue the value to be compared against
  49. * @return the equivalence of two strings, `inputValue` and `comparableValue`
  50. */
  51. public Boolean compareTwoStrings(String inputValue, String comparableValue){
  52. String inputValue1 = inputValue;
  53. String comparableValue1 = comparableValue;
  54. return inputValue1.equals(comparableValue);
  55. }
  56. /**
  57. * @param inputValue the value input from user
  58. * @return the middle character of `inputValue`
  59. */
  60. public Character getMiddleCharacter(String inputValue)
  61. {
  62. int mid = inputValue.length() / 2;
  63. char middleChar = inputValue.charAt(mid);
  64. if (inputValue.length() % 2 == 0){
  65. middleChar = inputValue.charAt(mid - 1);
  66. }
  67. return middleChar;
  68. }
  69. /**
  70. * @param spaceDelimitedString a string, representative of a sentence, containing spaces
  71. * @return the first sequence of characters
  72. */
  73. public String getFirstWord(String spaceDelimitedString){
  74. int a = spaceDelimitedString.indexOf(" ");
  75. String firstWord = spaceDelimitedString.substring(0, a);
  76. return firstWord;
  77. }
  78. /**
  79. * @param spaceDelimitedString a string delimited by spaces
  80. * @return the second word of a string delimited by spaces.
  81. */
  82. public String getSecondWord(String spaceDelimitedString){
  83. int start = spaceDelimitedString.indexOf(" ") + 1;
  84. int end = spaceDelimitedString.indexOf(" ", start);
  85. if (end == -1) {
  86. end = spaceDelimitedString.length();
  87. }
  88. String secondWord = spaceDelimitedString.substring(start, end);
  89. return secondWord;
  90. }
  91. /**
  92. * @param stringToReverse
  93. * @return an identical string with characters in reverse order.
  94. */
  95. public String reverse(String stringToReverse){
  96. if (stringToReverse == null || stringToReverse.isEmpty()) {
  97. return stringToReverse;
  98. }
  99. String reversed = "";
  100. for(int i = stringToReverse.length() - 1; i >= 0; i--) {
  101. reversed = reversed + stringToReverse.charAt(i);
  102. }
  103. return reversed;
  104. }
  105. /**
  106. * @param input
  107. * @return an identical string with spaces removed.
  108. */
  109. public String removeWhitespace(String input){
  110. String newTrim = input.replaceAll("\\s","");
  111. return newTrim;
  112. }
  113. /**
  114. * @param input
  115. * @return an identical string with spaces in the front and end removed.
  116. */
  117. public String trim(String input){
  118. String trimmedWord = input.trim();
  119. return trimmedWord;
  120. }
  121. }