/** * Created by dan on 6/14/17. */ public class StringUtilities { /** * @return `Hello World` as a string */ public static String getHelloWorld() { String hello = "Hello World"; return hello; } /** * @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){ String joined = firstSegment.concat(secondSegment); return joined; } /** * @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){ String joined = new String(); joined += "" + firstSegment; joined += secondSegment; return joined; } /** * @param input a string to be manipulated * @return the first 3 characters of `input` */ public static String getPrefix(String input){ String prefix = new String(); prefix = input.substring(0,3); return prefix; } /** * @param input a string to be manipulated * @return the last 3 characters of `input` */ public static String getSuffix(String input){ String suffix = new String(); suffix = input.substring(input.length()-3,input.length()); return suffix; } /** * @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){ char middle; int pos, len; char ch[] = inputValue.toCharArray(); len = ch.length; if(ch.length%2 == 0){ pos = len/2 - 1; } else{ pos = len/2; } middle = ch[pos]; return middle; } /** * @param spaceDelimitedString a string, representative of a sentence, containing spaces * @return the first sequence of characters */ public static String getFirstWord(String spaceDelimitedString){ int index = spaceDelimitedString.indexOf(' '); String word = spaceDelimitedString.substring(0,index); return word; } /** * @param spaceDelimitedString a string delimited by spaces * @return the second word of a string delimited by spaces. */ public static String getSecondWord(String spaceDelimitedString){ String str = spaceDelimitedString; int index = str.indexOf(' '); String word = new String(); if(str.indexOf(' ', index+1)!=-1){ int index2 = str.indexOf(' ', index+1); word = str.substring(index+1,index2); }else if(str.indexOf(' ', index+1)==-1){ word = str.substring(index+1, str.length()); }else{ word = str.substring(0,index); } return word; } /** * @param stringToReverse * @return an identical string with characters in reverse order. */ public static String reverse(String stringToReverse){ char ch[] = stringToReverse.toCharArray(); String reversed = new String(); for(int i=(ch.length)-1; i>=0; i--){ reversed += ch[i]; } return reversed; } }