implement a whole bunch of simple methods.

StringUtilities.java 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 result = firstSegment.concat(secondSegment);
  18. return result;
  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 number = Integer.toString(firstSegment);
  27. String result = number.concat(secondSegment);
  28. return result;
  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 str = input.substring(0,3);
  36. return str;
  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 str = input.substring(input.length()-3);
  44. return str;
  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. Boolean result = inputValue.equals(comparableValue);
  53. return result;
  54. }
  55. /**
  56. * @param inputValue the value input from user
  57. * @return the middle character of `inputValue`
  58. */
  59. public Character getMiddleCharacter(String inputValue){
  60. char midChar = inputValue.charAt(inputValue.length()/2);
  61. return midChar;
  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. String secondWord = spaceDelimitedString.split("\\s+")[1];
  78. return secondWord;
  79. }
  80. /**
  81. * @param stringToReverse
  82. * @return an identical string with characters in reverse order.
  83. */
  84. public String reverse(String stringToReverse){
  85. StringBuilder builder = new StringBuilder(stringToReverse);
  86. builder.reverse();
  87. return builder.toString();
  88. }
  89. /**
  90. * @param input
  91. * @return an identical string with spaces removed.
  92. */
  93. public String removeWhitespace(String input){
  94. return input.replaceAll(" ","");
  95. }
  96. /**
  97. * @param input
  98. * @return an identical string with spaces in the front and end removed.
  99. */
  100. public String trim(String input){
  101. String word = input.trim();
  102. return word;
  103. }
  104. }