fork(3) download
  1. public class Main
  2. {
  3. public static void main (String[] args) throws java.lang.Exception
  4. {
  5. String s1 = "Remove Last CharacterY";
  6. String s2 = "Remove Last Character2";
  7. String s3 = "N";
  8. String s4 = null;
  9. String s5 = "";
  10. System.out.println("After removing s1==" + removeLastChar(s1) + "==");
  11. System.out.println("After removing s2==" + removeLastChar(s2) + "==");
  12. System.out.println("After removing s3==" + removeLastChar(s3) + "==");
  13. System.out.println("After removing s4==" + removeLastChar(s4) + "==");
  14. System.out.println("After removing s5==" + removeLastChar(s5) + "==");
  15. }
  16.  
  17. public static String removeLastChar(String str) {
  18. return removeChars(str, 1);
  19. }
  20.  
  21. public static String removeChars(String str, int numberOfCharactersToRemove) {
  22. if(str != null && !str.trim().isEmpty()) {
  23. return str.substring(0, str.length() - numberOfCharactersToRemove);
  24. }
  25. return "";
  26. }
  27. }
Success #stdin #stdout 0.13s 49928KB
stdin
Standard input is empty
stdout
After removing s1==Remove Last Character==
After removing s2==Remove Last Character==
After removing s3====
After removing s4====
After removing s5====