implement a whole bunch of simple methods.

StringUtilities.java 3.6KB

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