How to remove the last character from a string in Java

1 Answer

0 votes
public class MyClass {
    private static String removeLastChar(String s) {
        String result = null;   
        if ((s != null) && (s.length() > 0)) {      
        	result = s.substring(0, s.length() - 1);   
        }   
        return result;
    }
    public static void main(String args[]) {
        String s = "java c c++ python";
        
        s = removeLastChar(s);
        
        System.out.println(s);
    }
}
 
 
 
/*
run:
 
java c c++ pytho
 
*/

 



answered Mar 22, 2021 by avibootz
edited Mar 22, 2021 by avibootz

Related questions

...