implement a whole bunch of simple methods.

StringUtilities.java 3.9KB

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