/** * This class contains numerous static methods for manipulating Strings. * * @author Dave Reed * @version 11/1/09 */ public class StringUtils { /** * Reverses a string. * @param str the string to be reversed * @return a copy of str with the order of the characters reversed */ public static String reverse(String str) { String copy = ""; for (int i = 0; i < str.length(); i++) { copy = str.charAt(i) + copy; } return copy; } /** * Strips all spaces out of a string. * @param str the string to be stripped * @return a copy of str with each space removed */ public static String stripSpaces(String str) { return stripAllOf(" ", str); } /** * Censors a string by replacing all vowels with asterisks. * @param str the string to be censored * @return a copy of str with each vowel replaced by an asterisk */ public static String censor(String str) { String copy = ""; for (int i = 0; i < str.length(); i++) { if (isVowel(str.charAt(i))) { copy += '*'; } else { copy += str.charAt(i); } } return copy; } /** * Capitalizes the first letter in the string. * @param str the string to be capitalized * @return a copy of str with the first letter capitalized */ public static String capitalize(String str) { return Character.toUpperCase(str.charAt(0)) + str.substring(1, str.length()); } /** * Translates a string into Pig Latin * @param str the string to be translated * @return a copy of str translated into Pig Latin */ public static String pigLatin(String str) { int firstVowel = findVowel(str); if (firstVowel <= 0) { return str + "way"; } else { return str.substring(firstVowel, str.length()) + str.substring(0,firstVowel) + "ay"; } } /** * Removes all non-letters from a string. * @param str the string to be stripped * @return a copy of str with all non-letters removed */ public static String stripNonLetters(String str) { String copy = ""; for (int i = 0; i < str.length(); i++) { if (Character.isLetter(str.charAt(i))) { copy += str.charAt(i); } } return copy; } /** * Determines if a character is a vowel (either upper or lower case). * @param ch the character to be tested * @return true if ch is a vowel, else false */ public static boolean isVowel(char ch) { return isOneOf(ch, "aeiouAEIOU"); } /** * Finds the first occurrence of a vowel in a string. * @param str the string to be searched * @return the index where the first vowel in str occurs (-1 if no vowel) */ public static int findVowel(String str) { return findOneOf("aeiouAEIOU", str); } /** * Determines whether a character is contained in a string. * @param ch the character to be searched for * @param str the string to be searched * @return true if ch is contained in str */ public static boolean isOneOf(char ch, String str) { return str.indexOf(ch) != -1; } /** * Finds the first occurrence of a character in a string * @param str the string to be searched * @param seq the sequence of characters to be searched for * @return the index of the first occurrence of a char from seq */ public static int findOneOf(String seq, String str) { for (int i = 0; i < str.length(); i++) { if (isOneOf(str.charAt(i), seq)) { return i; } } return -1; } /** * Removes all occurrences of select letters from a string. * @param str the string to be searched and modified * @param seq the sequence of characters to be removed * @return a copy of str with all chars from seq removed */ public static String stripAllOf(String seq, String str) { String copy = ""; for (int i = 0; i < str.length(); i++) { if (!isOneOf(str.charAt(i), seq)) { copy += str.charAt(i); } } return copy; } }