123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 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 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 ((int)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. String upToCharacters = input.substring(0,3);
  33. System.out.println(upToCharacters);
  34. return upToCharacters;
  35. }
  36. /**
  37. * @param input a string to be manipulated
  38. * @return the last 3 characters of `input`
  39. */
  40. public static String getSuffix(String input){
  41. String upToCharacters = input.substring(input.length()-3,input.length());
  42. return upToCharacters;
  43. }
  44. /**
  45. * @param inputValue the value to be compared
  46. * @param comparableValue the value to be compared against
  47. * @return the equivalence of two strings, `inputValue` and `comparableValue`
  48. */
  49. public static Boolean compareTwoStrings(String inputValue, String comparableValue){
  50. if(inputValue == comparableValue){
  51. return true;
  52. } else {
  53. return false;
  54. }
  55. }
  56. /**
  57. * @param inputValue the value input from user
  58. * @return the middle character of `inputValue`
  59. */
  60. public static Character getMiddleCharacter(String inputValue){
  61. int position;
  62. int length;
  63. if(inputValue.length() % 2 == 0) {
  64. position = inputValue.length() / 2 - 1;
  65. length = 2;
  66. } else {
  67. position = inputValue.length() / 2;
  68. length = 1;
  69. }
  70. char middleCharacter = inputValue.charAt(position);
  71. return middleCharacter;
  72. }
  73. /**
  74. * @param spaceDelimitedString a string, representative of a sentence, containing spaces
  75. * @return the first sequence of characters
  76. */
  77. public static String getFirstWord(String spaceDelimitedString){
  78. String myString = spaceDelimitedString;
  79. String arr[] = myString.split(" ");
  80. return arr[0];
  81. }
  82. /**
  83. * @param spaceDelimitedString a string delimited by spaces
  84. * @return the second word of a string delimited by spaces.
  85. */
  86. public static String getSecondWord(String spaceDelimitedString){
  87. String myString = spaceDelimitedString;
  88. String arr[] = myString.split(" ");
  89. return arr[1];
  90. }
  91. /**
  92. * @param stringToReverse
  93. * @return an identical string with characters in reverse order.
  94. */
  95. public static String reverse(String stringToReverse){
  96. String answer = new StringBuilder(stringToReverse).reverse().toString();
  97. return answer;
  98. }
  99. }