StringUtilities.java 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /**
  2. * Created by dan on 6/14/17.
  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. String n = Integer.toString(firstSegment);
  26. return n + 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(input.length() - 3);
  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. return inputValue.equals(comparableValue) ;
  49. }
  50. /**
  51. * @param inputValue the value input from user
  52. * @return the middle character of `inputValue`
  53. */
  54. public static Character getMiddleCharacter(String inputValue){
  55. int post;
  56. int leng;
  57. if (inputValue.length()% 2 == 1){
  58. post=inputValue.length()/2;
  59. leng= 1;
  60. } else {
  61. post=inputValue.length()/2-1;
  62. leng = 2;
  63. }
  64. String mid = inputValue.substring(post, post
  65. + leng);
  66. char output = mid.charAt(0);
  67. return output;//inputValue.charAt(inputValue.length()/2);
  68. }
  69. /**
  70. a * @param spaceDelimitedString a string, representative of a sentence, containing spaces
  71. * @return the first sequence of characters
  72. */
  73. public static String getFirstWord(String spaceDelimitedString){
  74. int str= spaceDelimitedString.indexOf(" ");
  75. String word = spaceDelimitedString.substring(0, str);
  76. return word ;
  77. }
  78. /**
  79. * @param spaceDelimitedString a string delimited by spaces
  80. * @return the second word of a string delimited by spaces.
  81. */
  82. public static String getSecondWord(String spaceDelimitedString){
  83. /**int word = spaceDelimitedString.indexOf(" ");
  84. int word1 = spaceDelimitedString.indexOf("", word );
  85. String secondWord=spaceDelimitedString.substring(word , word1);
  86. return secondWord;*/
  87. return spaceDelimitedString.split(" ")[1];
  88. }
  89. /**
  90. * @param stringToReverse
  91. * @return an identical string with characters in reverse order.
  92. */
  93. public static String reverse(String stringToReverse){
  94. String reverse="";
  95. for(int i = stringToReverse.length() - 1; i >= 0; i--)
  96. {
  97. reverse = reverse+ stringToReverse.charAt(i);
  98. }
  99. return reverse;
  100. }
  101. }