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
What's the Advantage of a Java Enum Versus a Class with Public Static Final Fields
Class Has Been Compiled by a More Recent Version of the Java Environment
How to Read and Understand the Java Stack Trace
Rationale for Matcher Throwing Illegalstateexception When No 'Matching' Method Is Called
Reversing a Linked List in Java, Recursively
Whitespace Matching Regex - Java
How Is an Instance Initializer Different from a Constructor
Format Double Value in Scientific Notation
In Java, How to Check If a String Contains a Substring (Ignoring Case)
How Do Shift Operators Work in Java
Purpose of a Constructor in Java
No Suitable Driver Found for 'Jdbc:Mysql://Localhost:3306/Mysql
Error Upon Assigning Layout: Boxlayout Can't Be Shared
Why Java Polymorphism Not Work in My Example
Performance of Java Matrix Math Libraries
Break When Exception Is Thrown