123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import java.lang.*;
  2. import java.io.*;
  3. import java.util.*;
  4. /**
  5. * Created by dan on 6/14/17.
  6. */
  7. public class StringUtilities {
  8. /**
  9. * @return `Hello World` as a string
  10. */
  11. public static String getHelloWorld() {
  12. String solution = "Hello World";
  13. return solution;
  14. }
  15. /**
  16. * @param firstSegment a string to be added to
  17. * @param secondSegment a string to add
  18. * @return the concatenation of two strings, `firstSegment`, and `secondSegment`
  19. */
  20. public static String concatenation(String firstSegment, String secondSegment){
  21. String solution = firstSegment + secondSegment;
  22. return solution;
  23. }
  24. /**
  25. * @param firstSegment a string to be added to
  26. * @param secondSegment a string to add
  27. * @return the concatenation of an integer, `firstSegment`, and a String, `secondSegment`
  28. */
  29. public static String concatenation(int firstSegment, String secondSegment){
  30. String convert = Integer.toString(firstSegment);
  31. String solution = convert + secondSegment;
  32. return solution;
  33. }
  34. /**
  35. * @param input a string to be manipulated
  36. * @return the first 3 characters of `input`
  37. */
  38. public static String getPrefix(String input){
  39. return input.substring(0,3);
  40. }
  41. /**
  42. * @param input a string to be manipulated
  43. * @return the last 3 characters of `input`
  44. */
  45. public static String getSuffix(String input){
  46. return input.substring(input.length() - 3);
  47. }
  48. /**
  49. * @param inputValue the value to be compared
  50. * @param comparableValue the value to be compared against
  51. * @return the equivalence of two strings, `inputValue` and `comparableValue`
  52. */
  53. public static Boolean compareTwoStrings(String inputValue, String comparableValue){
  54. if (inputValue == comparableValue) {
  55. return true;} else {
  56. return false;}
  57. }
  58. /**
  59. * @param inputValue the value input from user
  60. * @return the middle character of `inputValue`
  61. */
  62. public static Character getMiddleCharacter(String inputValue){
  63. int length = inputValue.length() - 1; // 6
  64. int middle = length / 2; //3
  65. String math = String.valueOf(middle + 1); //4
  66. int halfway = Integer.valueOf(math); //4
  67. String thechar = inputValue.substring(halfway - 1, halfway);
  68. char itsthechar = thechar.charAt(0);
  69. return itsthechar;
  70. }
  71. /**
  72. * @param spaceDelimitedString a string, representative of a sentence, containing spaces
  73. * @return the first sequence of characters
  74. */
  75. public static String getFirstWord(String spaceDelimitedString){
  76. String[] splitem = spaceDelimitedString.split("\\s+");
  77. return splitem[0];
  78. }
  79. /**
  80. * @param spaceDelimitedString a string delimited by spaces
  81. * @return the second word of a string delimited by spaces.
  82. */
  83. public static String getSecondWord(String spaceDelimitedString){
  84. String[] splitem = spaceDelimitedString.split("\\s+");
  85. return splitem[1];
  86. }
  87. /**
  88. * @param stringToReverse
  89. * @return an identical string with characters in reverse order.
  90. */
  91. public static String reverse(String stringToReverse){
  92. StringBuilder easy = new StringBuilder();
  93. easy.append(stringToReverse);
  94. return easy.reverse().toString();
  95. }
  96. }