implement a whole bunch of simple methods.

StringUtilities.java 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 newString;
  18. return newString = firstSegment + secondSegment;
  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 newString;
  27. return newString = Integer.toString(firstSegment) + secondSegment;
  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. return input.substring(0,3);
  35. }
  36. /**
  37. * @param input a string to be manipulated
  38. * @return the last 3 characters of `input`
  39. */
  40. public String getSuffix(String input){
  41. return input.substring((input.length()-3),input.length());
  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. if (inputValue.length() % 2 != 0){
  57. return inputValue.charAt(inputValue.length()/2);
  58. } else {
  59. return inputValue.charAt(inputValue.length()/2 - 1);
  60. }
  61. }
  62. /**
  63. * @param spaceDelimitedString a string, representative of a sentence, containing spaces
  64. * @return the first sequence of characters
  65. */
  66. public String getFirstWord(String spaceDelimitedString){
  67. String[] parts = spaceDelimitedString.split(" ");
  68. return parts[0];
  69. }
  70. /**
  71. * @param spaceDelimitedString a string delimited by spaces
  72. * @return the second word of a string delimited by spaces.
  73. */
  74. public String getSecondWord(String spaceDelimitedString){
  75. String[] parts = spaceDelimitedString.split(" ");
  76. return parts[1];
  77. }
  78. /**
  79. * @param stringToReverse
  80. * @return an identical string with characters in reverse order.
  81. */
  82. public String reverse(String stringToReverse){
  83. String reverseString="";
  84. for (int i=stringToReverse.length()-1; i>=0; i--){
  85. reverseString+=stringToReverse.charAt(i);
  86. }
  87. return reverseString;
  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. return input.trim();
  102. }
  103. }