123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import java.lang.*;
  2. /**
  3. * Created by dan on 6/14/17.
  4. */
  5. public class StringUtilities {
  6. /**
  7. * @return `Hello World` as a string
  8. */
  9. public static String getHelloWorld() {
  10. String hello = "Hello World";
  11. return hello;
  12. }
  13. /**
  14. * @param firstSegment a string to be added to
  15. * @param secondSegment a string to add
  16. * @return the concatenation of two strings, `firstSegment`, and `secondSegment`
  17. */
  18. public static String concatenation(String firstSegment, String secondSegment){
  19. String together = firstSegment + secondSegment;
  20. return together;
  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. String together = firstSegment + secondSegment;
  29. return together;
  30. }
  31. /**
  32. * @param input a string to be manipulated
  33. * @return the first 3 characters of `input`
  34. */
  35. public static String getPrefix(String input){
  36. String output = "";
  37. for(int i = 0; i <= 2 ; i++){
  38. output = output + input.charAt(i);
  39. }
  40. return output;
  41. }
  42. /**
  43. * @param input a string to be manipulated
  44. * @return the last 3 characters of `input`
  45. */
  46. public static String getSuffix(String input){
  47. String output = "";
  48. for(int i = input.length() - 3; i < input.length(); i ++){
  49. output = output + input.charAt(i);
  50. }
  51. return output;
  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. boolean output = false;
  60. if(inputValue.equals(comparableValue)){
  61. output = true;
  62. }else{
  63. output = false;
  64. }
  65. return output;
  66. }
  67. /**
  68. * @param inputValue the value input from user
  69. * @return the middle character of `inputValue`
  70. */
  71. public static Character getMiddleCharacter(String inputValue){
  72. int middle = 0;
  73. char middleChar = 'a';
  74. if(inputValue.length() % 2 == 0){
  75. middle = (inputValue.length() / 2) - 1;
  76. middleChar = inputValue.charAt(middle);
  77. } else if(inputValue.length() % 2 == 1){
  78. middle = inputValue.length() / 2;
  79. middleChar = inputValue.charAt(middle);
  80. }
  81. return middleChar;
  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. int space = spaceDelimitedString.indexOf(' ');
  89. String firstWord = spaceDelimitedString.substring(0,space);
  90. return firstWord;
  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. int space = spaceDelimitedString.indexOf(' ');
  98. String secondWord = spaceDelimitedString.substring(space+ 1, spaceDelimitedString.length());
  99. return secondWord;
  100. }
  101. /**
  102. * @param stringToReverse
  103. * @return an identical string with characters in reverse order.
  104. */
  105. public static String reverse(String stringToReverse){
  106. String output = "";
  107. for(int i = stringToReverse.length() - 1; i >= 0; i --){
  108. output = output + stringToReverse.charAt(i);
  109. }
  110. return output;
  111. }
  112. }