StringUtilities.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /**
  2. * Created by Curtis on 10/18/18.
  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 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. return input.substring(0, 3);
  33. }
  34. /**
  35. * @param input a string to be manipulated
  36. * @return the last 3 characters of `input`
  37. */
  38. public static String getSuffix(String input){
  39. return input.substring(input.length() - 3);
  40. }
  41. /**
  42. * @param inputValue the value to be compared
  43. * @param comparableValue the value to be compared against
  44. * @return the equivalence of two strings, `inputValue` and `comparableValue`
  45. */
  46. public static Boolean compareTwoStrings(String inputValue, String comparableValue){
  47. return inputValue.equals(comparableValue);
  48. }
  49. /**
  50. * @param inputValue the value input from user
  51. * @return the middle character of `inputValue`
  52. */
  53. public static Character getMiddleCharacter(String inputValue){
  54. int halfOfStringLength = 0;
  55. if(inputValue.length() % 2 == 0) {
  56. halfOfStringLength = (inputValue.length() / 2) - 1;
  57. } else {
  58. halfOfStringLength = inputValue.length() / 2;
  59. }
  60. char middleCharacter = inputValue.charAt(halfOfStringLength);
  61. return middleCharacter;
  62. }
  63. /**
  64. * @param spaceDelimitedString a string, representative of a sentence, containing spaces
  65. * @return the first sequence of characters
  66. */
  67. public static String getFirstWord(String spaceDelimitedString){
  68. String firstWord = " ";
  69. for(int i = 0; i < spaceDelimitedString.length(); i++) {
  70. if(Character.isWhitespace(spaceDelimitedString.charAt(i))) {
  71. firstWord = spaceDelimitedString.substring(0, i);
  72. break;
  73. }
  74. }
  75. return firstWord;
  76. }
  77. /**
  78. * @param spaceDelimitedString a string delimited by spaces
  79. * @return the second word of a string delimited by spaces.
  80. */
  81. public static String getSecondWord(String spaceDelimitedString){
  82. String secondWord = " ";
  83. int beginningOfSecondWord = 0;
  84. //int endOfSecondWord = 0;
  85. for(int i = 0; i < spaceDelimitedString.length(); i++) {
  86. if(Character.isWhitespace(spaceDelimitedString.charAt(i))) {
  87. beginningOfSecondWord = i;
  88. break;
  89. }
  90. }
  91. /*
  92. spaceDelimitedString = spaceDelimitedString.substring(beginningOfSecondWord + 1, spaceDelimitedString.length() - 1);
  93. for(int i = 0; i < spaceDelimitedString.length(); i++) {
  94. if(!Character.isWhitespace(spaceDelimitedString.charAt(i))) {
  95. endOfSecondWord = i;
  96. break;
  97. }
  98. }
  99. secondWord = spaceDelimitedString.substring(beginningOfSecondWord, endOfSecondWord);
  100. */
  101. secondWord = spaceDelimitedString.substring(beginningOfSecondWord + 1, spaceDelimitedString.length());
  102. return secondWord;
  103. }
  104. /**
  105. * @param stringToReverse
  106. * @return an identical string with characters in reverse order.
  107. */
  108. public static String reverse(String stringToReverse){
  109. StringBuilder reverseOrder = new StringBuilder();
  110. for(int i = 0; i < stringToReverse.length(); i++) {
  111. reverseOrder.insert(0, stringToReverse.charAt(i));
  112. }
  113. return reverseOrder.toString();
  114. }
  115. }