implement a whole bunch of simple methods.

StringUtilities.java 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 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 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 String concatenation(int firstSegment, String secondSegment){
  25. return Integer.toString(firstSegment)+secondSegment;
  26. }
  27. /**
  28. * @param input a string to be manipulated
  29. * @return the first 3 characters of `input`
  30. */
  31. public String getPrefix(String input){
  32. String k=input.substring(0,3);
  33. return k;
  34. }
  35. /**
  36. * @param input a string to be manipulated
  37. * @return the last 3 characters of `input`
  38. */
  39. public String getSuffix(String input){
  40. int len=input.length();
  41. int j=len-3;
  42. String k=input.substring(j);
  43. return k;
  44. }
  45. /**
  46. * @param inputValue the value to be compared
  47. * @param comparableValue the value to be compared against
  48. * @return the equivalence of two strings, `inputValue` and `comparableValue`
  49. */
  50. public Boolean compareTwoStrings(String inputValue, String comparableValue){
  51. Boolean k=inputValue.equals(comparableValue);
  52. return k;
  53. }
  54. /**
  55. * @param inputValue the value input from user
  56. * @return the middle character of `inputValue`
  57. */
  58. public Character getMiddleCharacter(String inputValue){
  59. return inputValue.charAt((inputValue.length()-1)/ 2);
  60. }
  61. /**
  62. * @param spaceDelimitedString a string, representative of a sentence, containing spaces
  63. * @return the first sequence of characters
  64. */
  65. public String getFirstWord(String spaceDelimitedString){
  66. int i = spaceDelimitedString.indexOf(' ');
  67. String word = spaceDelimitedString.substring(0, i);
  68. return word;
  69. }
  70. /**
  71. * @param spaceDelimitedString a string delimited by spaces
  72. * @return the second word of a string delimited by spaces.
  73. */
  74. public String getSecondWord(String spaceDelimitedString){
  75. String secondWord = spaceDelimitedString.split("\\s+")[1];
  76. return secondWord;
  77. }
  78. /**
  79. * @param stringToReverse
  80. * @return an identical string with characters in reverse order.
  81. */
  82. public String reverse(String stringToReverse){
  83. String k=new StringBuilder(stringToReverse).reverse().toString();
  84. return k;
  85. }
  86. /**
  87. * @param input
  88. * @return an identical string with spaces removed.
  89. */
  90. public String removeWhitespace(String input){
  91. return input.replaceAll(" ","");
  92. }
  93. /**
  94. * @param input
  95. * @return an identical string with spaces in the front and end removed.
  96. */
  97. public String trim(String input){
  98. return input.trim();
  99. }
  100. }