implement a whole bunch of simple methods.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /**
  2. * Created by dan on 6/14/17.
  3. */
  4. //import java.util.Arrays;
  5. import java.lang.String;
  6. public class StringUtilities {
  7. /**
  8. * @return `Hello World` as a string
  9. *
  10. */
  11. String temp = "";
  12. public String getHelloWorld() {
  13. return "Hello World";
  14. }
  15. /**
  16. * @param firstSegment a string to be added to
  17. * @param secondSegment a string to add
  18. * @return the concatenation of two strings, `firstSegment`, and `secondSegment`
  19. */
  20. public String concatenation(String firstSegment, String secondSegment){
  21. return firstSegment + secondSegment;
  22. }
  23. /**
  24. * @param firstSegment a string to be added to
  25. * @param secondSegment a string to add
  26. * @return the concatenation of an integer, `firstSegment`, and a String, `secondSegment`
  27. */
  28. public String concatenation(int firstSegment, String secondSegment){
  29. String newSegement = Integer.toString(firstSegment);
  30. return newSegement + secondSegment;
  31. }
  32. /**
  33. * @param input a string to be manipulated
  34. * @return the first 3 characters of `input`
  35. */
  36. public String getPrefix(String input){
  37. return input.substring(0,3);
  38. }
  39. /**
  40. * @param input a string to be manipulated
  41. * @return the last 3 characters of `input`
  42. */
  43. public String getSuffix(String input){
  44. String newString = input.substring(input.length()-3);
  45. return newString;
  46. }
  47. /**
  48. * @param inputValue the value to be compared
  49. * @param comparableValue the value to be compared against
  50. * @return the equivalence of two strings, `inputValue` and `comparableValue`
  51. */
  52. public Boolean compareTwoStrings(String inputValue, String comparableValue){
  53. Boolean newString = inputValue.equals(comparableValue);
  54. return newString;
  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. int mid = inputValue.length()/2;
  62. if(inputValue.length() % 2 !=0)
  63. {
  64. return inputValue.charAt(mid);
  65. }
  66. else
  67. {
  68. return inputValue.charAt(mid-1);
  69. }
  70. }
  71. /**
  72. * @param spaceDelimitedString a string, representative of a sentence, containing spaces
  73. * @return the first sequence of characters
  74. */
  75. public String getFirstWord(String spaceDelimitedString){
  76. return spaceDelimitedString.split(" ")[0];
  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. return spaceDelimitedString.split(" ")[1];
  84. }
  85. /**
  86. * @param stringToReverse
  87. * @return an identical string with characters in reverse order.
  88. */
  89. public String reverse(String stringToReverse){
  90. StringBuilder builder = new StringBuilder();
  91. for(int i = stringToReverse.length()-1;i >=0 ;i--)
  92. {
  93. builder.append(stringToReverse.charAt(i));
  94. }
  95. return builder.toString();
  96. }
  97. /**
  98. * @param input
  99. * @return an identical string with spaces removed.
  100. */
  101. public String removeWhitespace(String input){
  102. input = input.replaceAll("\\s","");
  103. return input;
  104. }
  105. /**
  106. * @param input
  107. * @return an identical string with spaces in the front and end removed.
  108. */
  109. public String trim(String input){
  110. return input.trim();
  111. }
  112. }