StringUtilities.java 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 static String getHelloWorld() {
  9. return "Hello Word";
  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 static 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 static String concatenation(int firstSegment, String secondSegment){
  25. return firstSegment+secondSegment;
  26. }
  27. /**
  28. * @param input a string to be manipulated
  29. * @return the first 3 characters of `input`
  30. */
  31. public static String getPrefix(String input){
  32. return input.substring(3);
  33. }
  34. /**
  35. * @param input a string to be manipulated
  36. * @return the last 3 characters of `input`
  37. */
  38. public static String getSuffix(String input){
  39. return input.substring(input.length()-4);
  40. }
  41. /**
  42. * @param inputValue the value to be compared
  43. * @param comparableValue the value to be compared against
  44. * @return the equivalence of two strings, `inputValue` and `comparableValue`
  45. */
  46. public static Boolean compareTwoStrings(String inputValue, String comparableValue){
  47. boolean result;
  48. result = inputValue == comparableValue ? true : false;
  49. return result;
  50. }
  51. /**
  52. * @param inputValue the value input from user
  53. * @return the middle character of `inputValue`
  54. */
  55. public static Character getMiddleCharacter(String inputValue){
  56. int midpoint;
  57. if(inputValue.length()%2!=0){
  58. midpoint = (inputValue.length()-1)/2;
  59. } else {
  60. midpoint = (inputValue.length()/2)-1;
  61. }
  62. return inputValue.charAt(midpoint);
  63. }
  64. /**
  65. * @param spaceDelimitedString a string, representative of a sentence, containing spaces
  66. * @return the first sequence of characters
  67. */
  68. public static String getFirstWord(String spaceDelimitedString){
  69. String word = spaceDelimitedString;
  70. int delim = word.indexOf(' ');
  71. return word.substring(0,delim);
  72. }
  73. /**
  74. * @param spaceDelimitedString a string delimited by spaces
  75. * @return the second word of a string delimited by spaces.
  76. */
  77. public static String getSecondWord(String spaceDelimitedString){
  78. String word = spaceDelimitedString;
  79. int delim = word.indexOf(' ');
  80. return word.substring(delim+1);
  81. }
  82. /**
  83. * @param stringToReverse
  84. * @return an identical string with characters in reverse order.
  85. */
  86. public static String reverse(String stringToReverse){
  87. StringBuilder builder = new StringBuilder();
  88. String word = stringToReverse;
  89. for (int i = word.length()-1; i>=0; i--){
  90. builder.append(word.charAt(i));
  91. }
  92. String result = builder.toString();
  93. System.out.println(result);
  94. return result;
  95. }
  96. }