/** * Created by Curtis on 10/18/18. */ public class StringUtilities { /** * @return `Hello World` as a string */ public static String getHelloWorld() { return "Hello World"; } /** * @param firstSegment a string to be added to * @param secondSegment a string to add * @return the concatenation of two strings, `firstSegment`, and `secondSegment` */ public static String concatenation(String firstSegment, String secondSegment){ return firstSegment + secondSegment; } /** * @param firstSegment a string to be added to * @param secondSegment a string to add * @return the concatenation of an integer, `firstSegment`, and a String, `secondSegment` */ public static String concatenation(int firstSegment, String secondSegment){ return firstSegment + secondSegment; } /** * @param input a string to be manipulated * @return the first 3 characters of `input` */ public static String getPrefix(String input){ return input.substring(0, 3); } /** * @param input a string to be manipulated * @return the last 3 characters of `input` */ public static String getSuffix(String input){ return input.substring(input.length() - 3); } /** * @param inputValue the value to be compared * @param comparableValue the value to be compared against * @return the equivalence of two strings, `inputValue` and `comparableValue` */ public static Boolean compareTwoStrings(String inputValue, String comparableValue){ return inputValue.equals(comparableValue); } /** * @param inputValue the value input from user * @return the middle character of `inputValue` */ public static Character getMiddleCharacter(String inputValue){ int halfOfStringLength = 0; if(inputValue.length() % 2 == 0) { halfOfStringLength = (inputValue.length() / 2) - 1; } else { halfOfStringLength = inputValue.length() / 2; } char middleCharacter = inputValue.charAt(halfOfStringLength); return middleCharacter; } /** * @param spaceDelimitedString a string, representative of a sentence, containing spaces * @return the first sequence of characters */ public static String getFirstWord(String spaceDelimitedString){ String firstWord = " "; for(int i = 0; i < spaceDelimitedString.length(); i++) { if(Character.isWhitespace(spaceDelimitedString.charAt(i))) { firstWord = spaceDelimitedString.substring(0, i); break; } } return firstWord; } /** * @param spaceDelimitedString a string delimited by spaces * @return the second word of a string delimited by spaces. */ public static String getSecondWord(String spaceDelimitedString){ String secondWord = " "; int beginningOfSecondWord = 0; //int endOfSecondWord = 0; for(int i = 0; i < spaceDelimitedString.length(); i++) { if(Character.isWhitespace(spaceDelimitedString.charAt(i))) { beginningOfSecondWord = i; break; } } /* spaceDelimitedString = spaceDelimitedString.substring(beginningOfSecondWord + 1, spaceDelimitedString.length() - 1); for(int i = 0; i < spaceDelimitedString.length(); i++) { if(!Character.isWhitespace(spaceDelimitedString.charAt(i))) { endOfSecondWord = i; break; } } secondWord = spaceDelimitedString.substring(beginningOfSecondWord, endOfSecondWord); */ secondWord = spaceDelimitedString.substring(beginningOfSecondWord + 1, spaceDelimitedString.length()); return secondWord; } /** * @param stringToReverse * @return an identical string with characters in reverse order. */ public static String reverse(String stringToReverse){ StringBuilder reverseOrder = new StringBuilder(); for(int i = 0; i < stringToReverse.length(); i++) { reverseOrder.insert(0, stringToReverse.charAt(i)); } return reverseOrder.toString(); } }