implement a whole bunch of simple methods.

Strings.java 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package Strings;
  2. /**
  3. * Created by dan on 6/14/17.
  4. */
  5. public class Strings {
  6. //Concatenate two strings together that are passed in the parameters
  7. public String concatenation(String one, String two){
  8. return one + two;
  9. }
  10. //Concatenate a string and a integer together that are passed in the parameters
  11. public String concatenation(int one, String two){
  12. return one + two;
  13. }
  14. //Get the substring of the first three letters of a string
  15. public String subStringBegin(String input){
  16. return input.substring(0,3);
  17. }
  18. //Get the substring of a string "Hello" so it returns the last three letters only
  19. public String subStringEnd(String input){
  20. return input.substring(input.length() - 3, input.length());
  21. }
  22. //Compare the two strings using compareTo() and if they are return true, else false
  23. public boolean compareTwoStrings(String one, String two){
  24. int comparison = one.compareTo(two);
  25. if (comparison == 0){
  26. return true;
  27. }
  28. else
  29. return false;
  30. }
  31. //Compare the two strings using equals() and if they are return true, else false
  32. public boolean compareTwoStringsEqual(String one, String two){
  33. return one.equals(two);
  34. }
  35. //Write a method that returns the middle character in the given string hint: use the .length and .charAt methods
  36. public char getTheMiddleChar(String string){
  37. Integer middle = string.length()/2;
  38. return string.charAt(middle);
  39. }
  40. //Use the indexOf method to find the first space in a string and .substring() to return the first word
  41. public String getTheFirstWord(String string){
  42. Integer space = 0;
  43. for (int i = 0; i < string.length(); i++){
  44. if (string.charAt(i) == ' '){
  45. space = i;
  46. }
  47. }
  48. return string.substring(0, space);
  49. }
  50. //Use the same behavior to find the second word
  51. public String getTheSecondWord(String string){
  52. Integer space = 0;
  53. for (int i = 0; i < string.length(); i++){
  54. if (string.charAt(i) == ' '){
  55. space = i;
  56. }
  57. }
  58. return string.substring(space + 1, string.length());
  59. }
  60. //Create a method that uses the above methods to return a string consisting of the second and first word in reversed order
  61. public String reverseTheTwo(String string){
  62. StringBuilder sb = new StringBuilder();
  63. sb.append(getTheSecondWord(string));
  64. sb.append(" ");
  65. sb.append(getTheFirstWord(string));
  66. return sb.toString();
  67. }
  68. }