|
| 1 | +public class StringUtil |
| 2 | +{ |
| 3 | + public static void main(String[] args) |
| 4 | + { |
| 5 | + |
| 6 | + String wordGiven = "goodbye"; |
| 7 | + |
| 8 | + String firstLetter = getFirstCharacter(wordGiven); |
| 9 | + System.out.println(firstLetter); |
| 10 | + |
| 11 | + String lastLetter = getLastCharacter(wordGiven); |
| 12 | + System.out.println(lastLetter); |
| 13 | + |
| 14 | + String firstTwo = getFirstTwoCharacters(wordGiven); |
| 15 | + System.out.println(firstTwo); |
| 16 | + |
| 17 | + String lastTwo = getLastTwoCharacters(wordGiven); |
| 18 | + System.out.println(lastTwo); |
| 19 | + |
| 20 | + String butFirstThree = getAllButFirstThreeCharacters(wordGiven); |
| 21 | + System.out.println(butFirstThree); |
| 22 | + |
| 23 | + printCharacters(wordGiven); |
| 24 | + |
| 25 | + printPhoneNumber("501-555-0100"); |
| 26 | + |
| 27 | + int first = findFirstE(wordGiven); |
| 28 | + System.out.println(first); |
| 29 | + |
| 30 | + String names = "Jake"; |
| 31 | + boolean finnOrJake = isFinn(names); |
| 32 | + System.out.println(finnOrJake); |
| 33 | + |
| 34 | + |
| 35 | + } |
| 36 | + |
| 37 | + public static String getFirstCharacter(String wordGiven) |
| 38 | + { |
| 39 | + char firstLetter = wordGiven.charAt(0); |
| 40 | + String stFirst = Character.toString(firstLetter); |
| 41 | + return stFirst; |
| 42 | + } |
| 43 | + |
| 44 | + public static String getLastCharacter(String wordGiven) |
| 45 | + { |
| 46 | + char lastLetter = wordGiven.charAt(wordGiven.length() - 1); |
| 47 | + String stLast = Character.toString(lastLetter); |
| 48 | + return stLast; |
| 49 | + } |
| 50 | + |
| 51 | + public static String getFirstTwoCharacters(String wordGiven) |
| 52 | + { |
| 53 | + String firstTwo = wordGiven.substring(0, 2); |
| 54 | + return firstTwo; |
| 55 | + } |
| 56 | + |
| 57 | + public static String getLastTwoCharacters(String wordGiven) |
| 58 | + { |
| 59 | + String lastTwo = wordGiven.substring(wordGiven.length() - 2, wordGiven.length()); |
| 60 | + return lastTwo; |
| 61 | + } |
| 62 | + |
| 63 | + public static String getAllButFirstThreeCharacters(String wordGiven) |
| 64 | + { |
| 65 | + //String allButFirstThree = wordGiven.substring(3); |
| 66 | + //return allButFirstThree; |
| 67 | + return wordGiven.substring(3); |
| 68 | + } |
| 69 | + |
| 70 | + public static void printCharacters(String wordGiven) |
| 71 | + { |
| 72 | + char givenChar; |
| 73 | + |
| 74 | + for(int i = 0; i < wordGiven.length(); i++) |
| 75 | + { |
| 76 | + givenChar = wordGiven.charAt(i); |
| 77 | + System.out.println(givenChar + ":" + i); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + public static void printPhoneNumber(String number) |
| 82 | + { |
| 83 | + String areaCode = number.substring(0, 3); |
| 84 | + System.out.println("Area Code: \t" + areaCode); |
| 85 | + |
| 86 | + String exchange = number.substring(4, 7); |
| 87 | + System.out.println("Area Code: \t" + exchange); |
| 88 | + |
| 89 | + String lineNumber = number.substring(8); |
| 90 | + System.out.println("Area Code: \t" + lineNumber); |
| 91 | + } |
| 92 | + |
| 93 | + public static int findFirstE(String wordGiven) |
| 94 | + { |
| 95 | + int indexOfE = wordGiven.indexOf("e"); |
| 96 | + return indexOfE; |
| 97 | + } |
| 98 | + |
| 99 | + public static boolean isFinn(String finnJake) |
| 100 | + { |
| 101 | + boolean fOrJ = true; |
| 102 | + if(finnJake.equals("Finn")) |
| 103 | + { |
| 104 | + fOrJ = true; |
| 105 | + } |
| 106 | + else |
| 107 | + { |
| 108 | + fOrJ = false; |
| 109 | + } |
| 110 | + |
| 111 | + return fOrJ; |
| 112 | + } |
| 113 | +} |
0 commit comments