Answers for "stringBuilder delete last character"

2

how to delete last char from stringbuilder

if(sb.length() > 0){
    sb.deleteCharAt(sb.length() - 1);
}
Posted by: Guest on February-27-2021
2

StringBuilder java pop back

sb.setLength(sb.length() - 1);
Posted by: Guest on June-15-2020
1

stringBuilder delete last character

public class StringBuilderRemoveLastCharacterExample {
 
    public static void main(String[] args) {
        
        //creates an empty StringBuilder object
        StringBuilder sbld = new StringBuilder();
        
        int[] numbers = {1, 2, 3, 4, 5};
        
        for(int number : numbers){
            sbld.append(number).append(",");
        }
        
        System.out.println("StringBuilder contains: " + sbld);
        
        /*
         * To remove last character from StringBuilder, use
         * the deleteCharAt method
         */
        
        //always make sure to check the length to avoid exception
        if( sbld.length() > 0 )
            sbld.deleteCharAt( sbld.length() - 1 );
        
        System.out.println("StringBuilder contains: " + sbld);
    }
}
Posted by: Guest on June-08-2021
5

string remove last character

public static String removeLastCharacter(String str) {   
	String result = null;   
    if ((str != null) && (str.length() > 0)) {      
    	result = str.substring(0, str.length() - 1);   
    }   
    
    return result;
}
Posted by: Guest on March-30-2020

Code answers related to "stringBuilder delete last character"

Browse Popular Code Answers by Language