/** * An introduction to Strings and String methods. * * @author Wilhem Alcivar */ public class StringParser { /** * Takes a String and returns that String with all characters uppercased. * E.G. cat would become CAT. dOnUt would become DONUT. * * @param s * @return String */ public static String upperCaseString(String s) { String uppercase = s.toUpperCase(); return uppercase; } /** * Takes a String and returns that String with all characters lowercased. * E.G. MOUSE would become mouse. dOnUt would become donut. * * @param s * @return String */ public static String lowerCaseString(String s) { String lowercase = s.toLowerCase(); return lowercase; } /** * Takes a String and returns the first character of that string. * E.G. cat would return c. Embark would return E. * * @param s * @return String */ public static Character getFirstCharacter(String s) { char firstChar = s.charAt(0); return firstChar; } /** * Takes a String and returns the character at index n of that string. * E.G. cat, 2 would return t. Embark, 4 would return r. * * @param s * @param n * @return String */ public static Character getNthCharacter(String s, Integer n) { char nthChar = s.charAt(n); return nthChar; } /** * Takes a String and returns that string with the first character uppercased. * E.G. cat would return Cat. cofFee would return CofFee. * * @param s * @return String */ public static String upperCaseFirstCharacter(String s) { String capitalized = s.substring(0, 1).toUpperCase() + s.substring(1); return capitalized; } /** * Takes a String and returns that string with the first character of each word in it uppercased * and then joined. * E.G. dog whistle would return DogWhistle. adjuNCT pRoFessOR would return AdjuctProfessor. * * @param s * @return String */ public static String camelCaseString(String s) { StringBuilder camelCaseSB = new StringBuilder(); String capitalFirst = s.substring(0, 1).toUpperCase(); camelCaseSB.append(capitalFirst); for (int i = 1; i < s.length(); i++) { if (s.charAt(i) == ' ') { String nextCapital = s.substring(i + 1, i+ 2).toUpperCase(); camelCaseSB.append(nextCapital); i++; } else { camelCaseSB.append(s.substring(i, i + 1).toLowerCase()); } } /* int firstSpace = s.indexOf(" "); String capitalSecond = s.substring(firstSpace + 1, firstSpace + 2).toUpperCase() + s.substring(1); String camelCaseString = capitalFirst.concat(capitalSecond); int secondSpace = s.indexOf(" ", firstSpace + 1); String secondWord; if (secondSpace > 0) { secondWord = s.substring(firstSpace, secondSpace); } else { secondWord = s.substring((firstSpace + 1),(s.length())); } */ String camelCase = camelCaseSB.toString(); return camelCase; } /** * Takes a String and returns that string with each character lowercased * and then joined with an underscore * E.G. dog whistle would return dog_whistle. adjuNCT pRoFessOR would return adjuct_professor. * * @param s * @return String */ public static String snakeCaseString(String s) { StringBuilder snakeCaseSB = new StringBuilder(); // String capitalFirst = s.substring(0, 1).toUpperCase(); // snakeCaseSB.append(capitalFirst); for (int i = 0; i < s.length(); i++) { if (s.charAt(i) == ' ') { snakeCaseSB.append('_'); } else { snakeCaseSB.append(s.substring(i, i + 1).toLowerCase()); } } String snakeCase = snakeCaseSB.toString(); return snakeCase; } /** * Takes a String and returns the length of that string * E.G. cat would return 3. extra would return 5. * * @param s * @return String */ public static Integer getLength(String s) { return s.length(); } /** * Takes in two strings and returns true if they are equal * E.G. example and shelf would return false. * cat and CaT would return false. * Dog and Dog would return true * * @param s1 * @param s2 * @return String */ public static Boolean isEqual(String s1, String s2) { boolean s3; if (s1.equals(s2)) { s3 = true; } else { s3 = false; } return s3; } /** * Takes in two strings and returns true if they are equal * E.G. example and shelf would return false. * cat and CaT would return true. * Dog and Dog would return true * * @param s1 * @param s2 * @return String */ public static Boolean isEqualIgnoreCase(String s1, String s2) { boolean s3; if (s1.equalsIgnoreCase(s2)) { s3 = true; } else { s3 = false; } return s3; } }