12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- public class StringUtilities {
-
-
- public Character getMiddleCharacter(String word){
- char[] character = word.toCharArray();
- int len = (character.length) / 2;
-
- if(character.length % 2 == 0)
- {
- return character[len];
- } else {
- return character[len];
- }
- }
-
- public String removeCharacter(String value, char charToRemove){
- //store input at the time this is called
- String n = value;
- //convert the to be removed char to a string
- String charString = String.valueOf(charToRemove);
- //replace the char with an empty string - removing it from the string
- //if there is nothing to remove, this function will return the original string
- n = searchReplaceFirst(n , charString, "");
- //if n is now still the same as the starting value, this means nothing could be replaced
- if(n.equals(value)){
- //return n since all occurences have been removed
- return n;
- //if n is now different from value, after the char has been replaced, recall this method.
- }else{
- //convert the string back to a character
- char remove = charString.charAt(0);
- //recursivly call this function on the updated string
- return removeCharacter(n, remove);
- }
- }
-
- public static String searchReplaceFirst(String value, String replace, String replacement){
- //if the two values are the same after something has been replaced, this means nothing has been replaced
- if(value.equals(value.replaceFirst(replace, replacement)))
- {
- //so return the original string
- return value;
- //otherwise, this means something has been replaced
- } else {
- //return the new value
- return value.replaceFirst(replace, replacement);
- }
- }
-
- public String getLastWord(String value) {
- String[] result = value.split(" ");
- return result[result.length - 1];
- }
- }
|