StringUtilities.java 3.3KB

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