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;
}
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);
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);
How can I remove the last character of a sentence in JavaScript?
Since the str.slice function returns the sliced part between two index as a substring, in your case, you should use str.slice(0,-1)
, that is equals to str.slice(0, str.length - 1)
. And it means that is going to return every character from zero index without the last character of the string passed.
function morseCode(str) {
morseCode = {
"A": ".- ",
"B": "-... ",
"C": "-.-. ",
"D": "-.. ",
"E": ". ",
"F": "..-. ",
"G": "--. ",
"H": ".... ",
"I": ".. ",
"J": ".--- ",
"K": "-.- ",
"L": ".-.. ",
"M": "-- ",
"N": "-. ",
"O": "--- ",
"P": ".--. ",
"Q": "--.- ",
"R": ".-. ",
"S": "... ",
"T": "- ",
"U": "..- ",
"W": ".-- ",
"X": "-..- ",
"Y": "-.-- ",
"Z": "--.. ",
'1':'.---- ',
'2':'..--- ',
'3':'...-- ',
'4':'....- ',
'5':'..... ',
'6':'-.... ',
'7':'--... ',
'8':'---.. ',
'9':'----. ',}
str = str.replace(/[!,?]/g , '');
str = str.replace(/\s/g, '');
return str.toUpperCase().split("").map(el => {
return morseCode[el] ? morseCode[el] : el;
}).join("").slice(0,-1); // this returns the string without the last character.
};
Trim last character from a string
"Hello! world!".TrimEnd('!');
read more
EDIT:
What I've noticed in this type of questions that quite everyone suggest to remove the last char of given string. But this does not fulfill the definition of Trim method.
Trim - Removes all occurrences of
white space characters from the
beginning and end of this instance.
MSDN-Trim
Under this definition removing only last character from string is bad solution.
So if we want to "Trim last character from string" we should do something like this
Example as extension method:
public static class MyExtensions
{
public static string TrimLastCharacter(this String str)
{
if(String.IsNullOrEmpty(str)){
return str;
} else {
return str.TrimEnd(str[str.Length - 1]);
}
}
}
Note if you want to remove all characters of the same value i.e(!!!!)the method above removes all existences of '!' from the end of the string,
but if you want to remove only the last character you should use this :
else { return str.Remove(str.Length - 1); }
Related Topics
Process.Close() Is Not Terminating Created Process,C#
Where Is My System.Numerics Namespace
Finding Property Differences Between Two C# Objects
Newtonsoft Inline Formatting for Subelement While Serializing
Generate Class from Database Table
Write Device Platform Specific Code in Xamarin.Forms
How to Get the Text of a Messagebox When It Has an Icon
How to Expand a String That Contains C# Literal Expressions at Runtime
Is There a Format Code Shortcut for Visual Studio
Binding to Custom Control Inside Datatemplate for Itemscontrol
C# Marking Class Property as Dirty
In What Areas Might the Use of F# Be More Appropriate Than C#
How to Delete a Row from Gridview
How to Use Acrylic Accent in Windows 10 Creators Update