'How to remove the first and the last character of a string
I'm wondering how to remove the first and last character of a string in Javascript.
My url is showing /installers/ and I just want installers.
Sometimes it will be /installers/services/ and I just need installers/services.
So I can't just simply strip the slashes /.
Solution 1:[1]
Here you go
var yourString = "/installers/";
var result = yourString.substring(1, yourString.length-1);
console.log(result);
Or you can use .slice as suggested by Ankit Gupta
var yourString = "/installers/services/";
var result = yourString.slice(1,-1);
console.log(result);
Solution 2:[2]
It may be nicer one to use slice like :
string.slice(1, -1)
Solution 3:[3]
I don't think jQuery has anything to do with this. Anyway, try the following :
url = url.replace(/^\/|\/$/g, '');
Solution 4:[4]
If you dont always have a starting or trailing slash, you could regex it. While regexes are slower then simple replaces/slices, it has a bit more room for logic:
"/installers/services/".replace(/^\/?|\/?$/g, "")
# /installers/services/ -> installers/services
# /installers/services -> installers/services
# installers/services/ -> installers/services
The regex explained:
- ['start with'
^] + [Optional?] + [slash]:^/?, escaped ->^\/? - The pipe ( | ) can be read as
or - ['ends with'
$] + [Optional?] + [slash] ->/?$, escaped ->\/?$
Combined it would be ^/?|/$ without escaping. Optional first slash OR optional last slash.
Technically it isn't "optional", but "zero or one times".
Solution 5:[5]
You can do something like that :
"/installers/services/".replace(/^\/+/g,'').replace(/\/+$/g,'')
This regex is a common way to have the same behaviour of the trim function used in many languages.
A possible implementation of trim function is :
function trim(string, char){
if(!char) char = ' '; //space by default
char = char.replace(/([()[{*+.$^\\|?])/g, '\\$1'); //escape char parameter if needed for regex syntax.
var regex_1 = new RegExp("^" + char + "+", "g");
var regex_2 = new RegExp(char + "+$", "g");
return string.replace(regex_1, '').replace(regex_2, '');
}
Which will delete all / at the beginning and the end of the string. It handles cases like ///installers/services///
You can also simply do :
"/installers/".substring(1, string.length-1);
Solution 6:[6]
You can use substring method
s = s.substring(0, s.length - 1) //removes last character
another alternative is slice method
Solution 7:[7]
if you need to remove the first leter of string
string.slice(1, 0)
and for remove last letter
string.slice(0, -1)
Solution 8:[8]
use .replace(/.*\/(\S+)\//img,"$1")
"/installers/services/".replace(/.*\/(\S+)\//img,"$1"); //--> services
"/services/".replace(/.*\/(\S+)\//img,"$1"); //--> services
Solution 9:[9]
url=url.substring(1,url.Length-1);
This way you can use the directories if it is like .../.../.../... etc.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Trott |
| Solution 2 | Penny Liu |
| Solution 3 | |
| Solution 4 | |
| Solution 5 | |
| Solution 6 | Hessam |
| Solution 7 | Rizwan |
| Solution 8 | Victor Ribeiro da Silva Eloy |
| Solution 9 | Kuzgun |
