StringUtilities.java 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import java.util.*;
  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. 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. return firstSegment + secondSegment;
  19. }
  20. /**
  21. * @param firstSegment a string to be added to
  22. * @param secondSegment a string to add
  23. * @return the concatenation of an integer, `firstSegment`, and a String, `secondSegment`
  24. */
  25. public static String concatenation(int firstSegment, String secondSegment){
  26. return (firstSegment + secondSegment);
  27. }
  28. /**
  29. * @param input a string to be manipulated
  30. * @return the first 3 characters of `input`
  31. */
  32. public static String getPrefix(String input){
  33. return input.substring(0,3);
  34. }
  35. /**
  36. * @param input a string to be manipulated
  37. * @return the last 3 characters of `input`
  38. */
  39. public static String getSuffix(String input){
  40. return input.substring(2,5);
  41. }
  42. /**
  43. * @param inputValue the value to be compared
  44. * @param comparableValue the value to be compared against
  45. * @return the equivalence of two strings, `inputValue` and `comparableValue`
  46. */
  47. public static Boolean compareTwoStrings(String inputValue, String comparableValue){
  48. if (inputValue.equals(comparableValue)){
  49. return true;
  50. }else{
  51. return false;
  52. }
  53. }
  54. /**
  55. * @param inputValue the value input from user
  56. * @return the middle character of `inputValue`
  57. */
  58. public static Character getMiddleCharacter(String inputValue){
  59. return inputValue.charAt((inputValue.length()-1)/2);
  60. }
  61. /**
  62. * @param spaceDelimitedString a string, representative of a sentence, containing spaces
  63. * @return the first sequence of characters
  64. */
  65. public static String getFirstWord(String spaceDelimitedString){
  66. return (spaceDelimitedString + " ").split(" ") [0];
  67. }
  68. /**
  69. * @param spaceDelimitedString a string delimited by spaces
  70. * @return the second word of a string delimited by spaces.
  71. */
  72. public static String getSecondWord(String spaceDelimitedString){
  73. return (spaceDelimitedString + " ").split(" ") [1];
  74. }
  75. /**
  76. * @param stringToReverse
  77. * @return an identical string with characters in reverse order.
  78. */
  79. public static String reverse(String stringToReverse){
  80. String input = stringToReverse;
  81. StringBuilder input1 = new StringBuilder(input);
  82. /* append a string into StringBuilder input1*/
  83. /*input1.append(input);
  84. /*reverse StringBuilder input1*/
  85. String result = input1.reverse().toString();
  86. /*print reversed string*/
  87. System.out.println(input1);
  88. return result;
  89. }
  90. }