StringUtilities.java 3.1KB

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