StringUtilities.java 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. String hello = "Hello World";
  10. return hello;
  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. String joined = firstSegment.concat(secondSegment);
  19. return joined;
  20. }
  21. /**
  22. * @param firstSegment a string to be added to
  23. * @param secondSegment a string to add
  24. * @return the concatenation of an integer, `firstSegment`, and a String, `secondSegment`
  25. */
  26. public static String concatenation(int firstSegment, String secondSegment){
  27. String joined = new String();
  28. joined += "" + firstSegment;
  29. joined += secondSegment;
  30. return joined;
  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. String prefix = new String();
  38. prefix = input.substring(0,3);
  39. return prefix;
  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. String suffix = new String();
  47. suffix = input.substring(input.length()-3,input.length());
  48. return suffix;
  49. }
  50. /**
  51. * @param inputValue the value to be compared
  52. * @param comparableValue the value to be compared against
  53. * @return the equivalence of two strings, `inputValue` and `comparableValue`
  54. */
  55. public static Boolean compareTwoStrings(String inputValue, String comparableValue){
  56. return inputValue.equals(comparableValue);
  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. char middle;
  64. int pos, len;
  65. char ch[] = inputValue.toCharArray();
  66. len = ch.length;
  67. if(ch.length%2 == 0){
  68. pos = len/2 - 1;
  69. } else{
  70. pos = len/2;
  71. }
  72. middle = ch[pos];
  73. return middle;
  74. }
  75. /**
  76. * @param spaceDelimitedString a string, representative of a sentence, containing spaces
  77. * @return the first sequence of characters
  78. */
  79. public static String getFirstWord(String spaceDelimitedString){
  80. int index = spaceDelimitedString.indexOf(' ');
  81. String word = spaceDelimitedString.substring(0,index);
  82. return word;
  83. }
  84. /**
  85. * @param spaceDelimitedString a string delimited by spaces
  86. * @return the second word of a string delimited by spaces.
  87. */
  88. public static String getSecondWord(String spaceDelimitedString){
  89. String str = spaceDelimitedString;
  90. int index = str.indexOf(' ');
  91. String word = new String();
  92. if(str.indexOf(' ', index+1)!=-1){
  93. int index2 = str.indexOf(' ', index+1);
  94. word = str.substring(index+1,index2);
  95. }else if(str.indexOf(' ', index+1)==-1){
  96. word = str.substring(index+1, str.length());
  97. }else{
  98. word = str.substring(0,index);
  99. }
  100. return word;
  101. }
  102. /**
  103. * @param stringToReverse
  104. * @return an identical string with characters in reverse order.
  105. */
  106. public static String reverse(String stringToReverse){
  107. char ch[] = stringToReverse.toCharArray();
  108. String reversed = new String();
  109. for(int i=(ch.length)-1; i>=0; i--){
  110. reversed += ch[i];
  111. }
  112. return reversed;
  113. }
  114. }