fork(1) download
  1. class TesteStrings {
  2. public static void main(String[] args) {
  3. String x = " PARA DAR TRIM ";
  4. System.out.println("Com ltrim: (" + ltrim(x) + ")");
  5. System.out.println("Com rtrim: (" + rtrim(x) + ")");
  6. }
  7.  
  8. public static String rtrim(String toTrim) {
  9. char[] val = toTrim.toCharArray();
  10. int len = val.length;
  11.  
  12. while (len > 0 && val[len - 1] <= ' ') {
  13. len--;
  14. }
  15. return len < val.length ? toTrim.substring(0, len) : toTrim;
  16. }
  17.  
  18. public static String ltrim(String toTrim) {
  19. int st = 0;
  20. char[] val = toTrim.toCharArray();
  21. int len = val.length;
  22.  
  23. while (st < len && val[st] <= ' ') {
  24. st++;
  25. }
  26. return st > 0 ? toTrim.substring(st, len) : toTrim;
  27. }
  28. }
Success #stdin #stdout 0.03s 320576KB
stdin
Standard input is empty
stdout
Com ltrim: (PARA DAR TRIM       )
Com rtrim: (       PARA DAR TRIM)