StringUtilities.java 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 x = "Hello World";
  10. return x;
  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 complete = firstSegment + secondSegment;
  19. return complete;
  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 complete = firstSegment + secondSegment;
  28. return complete;
  29. }
  30. /**
  31. * @param input a string to be manipulated
  32. * @return the first 3 characters of `input`
  33. */
  34. public static String getPrefix(String input){
  35. if(input.length()<= 3){
  36. return input;
  37. }
  38. return input.substring(0,3);
  39. }
  40. /**
  41. * @param input string to be manipulated
  42. * @return the last 3 characters of `input`
  43. */
  44. public static String getSuffix(String input){
  45. /*
  46. for(int i = input.length(); i>=0; i-- ){
  47. hold= hold + input.charAt(i);
  48. }
  49. */
  50. return input.substring(input.length()-3,input.length());
  51. }
  52. /**
  53. * @param inputValue the value to be compared
  54. * @param comparableValue the value to be compared against
  55. * @return the equivalence of two strings, `inputValue` and `comparableValue`
  56. */
  57. public static Boolean compareTwoStrings(String inputValue, String comparableValue){
  58. if(inputValue.equals(comparableValue)){
  59. return true;
  60. }
  61. return false;
  62. }
  63. /**
  64. * @param inputValue the value input from user
  65. * @return the middle character of `inputValue`
  66. */
  67. public static Character getMiddleCharacter(String inputValue){
  68. int middle;
  69. char sum1;
  70. if( inputValue.length() % 2 == 0){
  71. middle = inputValue.length()/2-1;
  72. sum1 = inputValue.charAt(middle);
  73. }else{
  74. middle = inputValue.length()/2;
  75. sum1 = inputValue.charAt(middle);
  76. }
  77. return sum1;
  78. }
  79. /**
  80. * @param spaceDelimitedString a string, representative of a sentence, containing spaces
  81. * @return the first sequence of characters
  82. */
  83. public static String getFirstWord(String spaceDelimitedString){
  84. /*
  85. int i= spaceDelimitedString.indexOf("");
  86. String wordsweneed = spaceDelimitedString.substring(i,spaceDelimitedString.length());
  87. String wordwewant = spaceDelimitedString.substring(i);
  88. return wordwewant;
  89. */
  90. return spaceDelimitedString.split(" ")[0];
  91. }
  92. /**
  93. * @param spaceDelimitedString a string delimited by spaces
  94. * @return the second word of a string delimited by spaces.
  95. */
  96. public static String getSecondWord(String spaceDelimitedString){
  97. int i= spaceDelimitedString.indexOf("");
  98. String wordsweneed = spaceDelimitedString.substring(i,spaceDelimitedString.length());
  99. return spaceDelimitedString.split(" ")[1];
  100. }
  101. /**
  102. * @param stringToReverse
  103. * @return an identical string with characters in reverse order.
  104. */
  105. public static String reverse(String stringToReverse){
  106. String hold = "";
  107. for(int i = stringToReverse.length()-1; i>=0; i-- ){
  108. hold= hold + stringToReverse.charAt(i);
  109. }
  110. return hold;
  111. }
  112. }