StringUtilities.java 1.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. public class StringUtilities {
  2. public Character getMiddleCharacter(String word){
  3. char midChar = word.charAt(word.length() / 2);
  4. return midChar;
  5. }
  6. public String removeCharacter(String value, char charToRemove){
  7. StringBuilder sb = new StringBuilder();
  8. for (int i = 0; i < value.length(); i++) {
  9. if (value.charAt(i) == charToRemove) {
  10. continue;
  11. } else {
  12. sb.append(value.charAt(i));
  13. }
  14. }
  15. String newValue = sb.toString();
  16. return newValue;
  17. }
  18. public String getLastWord(String value) {
  19. // Cycle through each element of the string.
  20. // If the element is a space, the word is the space
  21. StringBuilder sbLastWord = new StringBuilder();
  22. for (int i = value.length() - 1; i > 0; i--) {
  23. if (value.charAt(i) == ' ') {
  24. sbLastWord.append(value.substring(i + 1, value.length()));
  25. break;
  26. }
  27. }
  28. String lastWord = sbLastWord.toString();
  29. return lastWord;
  30. }
  31. }