fork download
  1. public class SubStringDemo{
  2.  
  3. public static void main(String args[]) {
  4.  
  5. // 1st example: You can use substring() method to remove first
  6. // and the last character from String in Java.
  7.  
  8. String text = "iMac";
  9. String withoutFirstCharacter = text.substring(1); // index starts at zero
  10. String withoutLastCharacter = text.substring(0, text.length() - 1);
  11.  
  12. System.out.println("Using SubString() method: ");
  13. System.out.println("input string: " + text);
  14. System.out.println("without first character: " + withoutFirstCharacter);
  15. System.out.println("without last character: " + withoutLastCharacter);
  16.  
  17.  
  18. // 2nd Example - You can use StringBuffer or StringBuilder to remove
  19. // first or last character from String in Java
  20. String iStore = "iCloud";
  21.  
  22. // converting String to StringBuilder
  23. StringBuilder builder = new StringBuilder(iStore);
  24.  
  25. // removing first character
  26. builder.deleteCharAt(0);
  27.  
  28. System.out.println("Using StringBuilder deleteCharAt() method: ");
  29. System.out.println("input string: " + iStore);
  30. System.out.println("String after removing first character: "
  31. + builder.toString());
  32.  
  33. // creating another StringBuilder
  34. builder = new StringBuilder(iStore);
  35.  
  36. // removing last character from String
  37. builder.deleteCharAt(iStore.length() - 1);
  38. System.out.println("String after removing last character: "
  39. + builder.toString());
  40. }
  41.  
  42. }
Success #stdin #stdout #stderr 0.01s 8924KB
stdin
Standard input is empty
stdout
Object: UndefinedObject error: did not understand #SubStringDemo
MessageNotUnderstood(Exception)>>signal (ExcHandling.st:254)
UndefinedObject class(Object)>>doesNotUnderstand: #SubStringDemo (SysExcept.st:1448)
UndefinedObject>>executeStatements (prog:1)
stderr
./prog:3: parse error, expected '}'