Last active
January 15, 2016 06:03
-
-
Save JoseRivas1998/eea70834fc204bdb53c5 to your computer and use it in GitHub Desktop.
Here is a bunch of String Algorithms, feel free to use them
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Allows you to remove punctuation from a string | |
| * @param s The String you will be removing the punctuation from | |
| * @return A new String without punctuation | |
| */ | |
| public static String removePunctuation(String s) { | |
| String removed = s; | |
| for(int i = 33; i <= 47; i++) { | |
| removed = removed.replace(String.valueOf((char) i), ""); | |
| } | |
| for(int i = 58; i <= 64; i++) { | |
| removed = removed.replace(String.valueOf((char) i), ""); | |
| } | |
| for(int i = 91; i <= 96; i++) { | |
| removed = removed.replace(String.valueOf((char) i), ""); | |
| } | |
| for(int i = 123; i <= 126; i++) { | |
| removed = removed.replace(String.valueOf((char) i), ""); | |
| } | |
| return removed; | |
| } | |
| /** | |
| * Returns the first word of the String, if the String is only one word, will return the string | |
| * @param s The String itself | |
| * @param trim Whether to trim the String before finding the first word | |
| * @param removePunctuation Whether to remove punctuation before finding the first word | |
| * @return The first word of the String | |
| */ | |
| public static final String firstWord(String s, boolean trim, boolean removePunctuation) { | |
| String stringCopy = s; | |
| if(trim) { | |
| stringCopy = stringCopy.trim(); | |
| } | |
| if(removePunctuation) { | |
| stringCopy = removePunctuation(stringCopy); | |
| } | |
| int index = stringCopy.indexOf(' '); | |
| String firstWord = ""; | |
| if(!hasMultipleWords(stringCopy)) { | |
| firstWord = stringCopy; | |
| } else { | |
| firstWord = stringCopy.substring(0, index); | |
| } | |
| return firstWord; | |
| } | |
| /** | |
| * Returns the last word of the String, if the String is only one word, will return the string | |
| * @param s The String itself | |
| * @param trim Whether to trim the String before finding the last word | |
| * @param removePunctuation Whether to remove punctuation before finding the last word | |
| * @return The last word of the String | |
| */ | |
| public static final String lastWord(String s, boolean trim, boolean removePunctuation) { | |
| String stringCopy = s; | |
| if(trim) { | |
| stringCopy = stringCopy.trim(); | |
| } | |
| if(removePunctuation) { | |
| stringCopy = removePunctuation(stringCopy); | |
| } | |
| int index = stringCopy.lastIndexOf(' '); | |
| String lastWord = ""; | |
| if(!hasMultipleWords(stringCopy)) { | |
| lastWord = stringCopy; | |
| } else { | |
| lastWord = stringCopy.substring(index + 1); | |
| } | |
| return lastWord; | |
| } | |
| /** | |
| * Tells whether the String has multiple words | |
| * @param s The String itself | |
| * @return Whether the String has multiple words | |
| */ | |
| public static boolean hasMultipleWords(String s) { | |
| String copy = s.trim(); | |
| return copy.indexOf(' ') != -1; | |
| } | |
| /** | |
| * Separates a String into each word in the string | |
| * @param s The String | |
| * @return An ArrayList<String> containing the words | |
| */ | |
| public static final ArrayList<String> wordsOfStringArrayList(String s) { | |
| ArrayList<String> list = new ArrayList<>(); | |
| String temp = removePunctuation(s).trim(); | |
| while(hasMultipleWords(temp)) { | |
| String word = firstWord(temp, true, true); | |
| list.add(word); | |
| int index = word.length(); | |
| if(temp.charAt(index) == ' ') { | |
| index++; | |
| } | |
| temp = temp.substring(index); | |
| } | |
| list.add(temp); | |
| return list; | |
| } | |
| /** | |
| * Separates a String into each word in the string | |
| * @param s The String | |
| * @return A String[] containing the words | |
| */ | |
| public static final String[] wordsOfStringArray(String s) { | |
| ArrayList<String> list = wordsOfStringArrayList(s); | |
| String[] array = new String[list.size()]; | |
| for(int i = 0; i < array.length; i++) { | |
| array[i] = list.get(i); | |
| } | |
| return array; | |
| } | |
| /** | |
| * Checks if a String contains another String <strong>Case Sensitive</strong> | |
| * @param initial The base String | |
| * @param check The String to check if in the base | |
| * @return Whether check is in Initial | |
| */ | |
| public static boolean containsString(String initial, String check) { | |
| return initial.indexOf(check) >= 0; | |
| } | |
| /** | |
| * Checks if a String contains another String <strong>Case Insensitive</strong> | |
| * @param initial The base String | |
| * @param check The String to check if in the base | |
| * @return Whether check is in Initial | |
| */ | |
| public static boolean containsStringIgnoreCase(String initial, String check) { | |
| String initLower = initial.toLowerCase(); | |
| String checkLower = check.toLowerCase(); | |
| return containsString(initLower, checkLower); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment