How to Remove the Last Character from a String

How to remove the last character from a string?

replace will replace all instances of a letter. All you need to do is use substring():

public String method(String str) {
if (str != null && str.length() > 0 && str.charAt(str.length() - 1) == 'x') {
str = str.substring(0, str.length() - 1);
}
return str;
}

Removing last character in C

Just set the last char to be '\0':

str[strlen(str)-1] = '\0';

In C, \0 indicates a string ending.

How to remove the first and the last character of a string

Here you go

var yourString = "/installers/";
var result = yourString.substring(1, yourString.length-1);

console.log(result);

Delete the last two characters of the String

Subtract -2 or -3 basis of removing last space also.

 public static void main(String[] args) {
String s = "apple car 05";
System.out.println(s.substring(0, s.length() - 2));
}

Output

apple car

How do I chop/slice/trim off last character in string using Javascript?

You can use the substring function:

let str = "12345.00";str = str.substring(0, str.length - 1);console.log(str);


Related Topics



Leave a reply



Submit