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

JavascriptString

Javascript Problem Overview


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 /.

Javascript Solutions


Solution 1 - Javascript

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);

Documentation for the slice and substring.

Solution 2 - Javascript

It may be nicer one to use slice like :

string.slice(1, -1)

Solution 3 - Javascript

I don't think jQuery has anything to do with this. Anyway, try the following :

url = url.replace(/^\/|\/$/g, '');

Solution 4 - Javascript

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 - Javascript

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 - Javascript

You can use substring method

s = s.substring(0, s.length - 1) //removes last character

another alternative is slice method

Solution 7 - Javascript

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 - Javascript

use .replace(/.*\/(\S+)\//img,"$1")

"/installers/services/".replace(/.*\/(\S+)\//img,"$1"); //--> services

"/services/".replace(/.*\/(\S+)\//img,"$1"); //--> services

Solution 9 - Javascript

url=url.substring(1,url.Length-1);

This way you can use the directories if it is like .../.../.../... etc.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPeanutView Question on Stackoverflow
Solution 1 - JavascriptDietergView Answer on Stackoverflow
Solution 2 - JavascriptAnkit GuptaView Answer on Stackoverflow
Solution 3 - Javascriptuser1636522View Answer on Stackoverflow
Solution 4 - JavascriptMartijnView Answer on Stackoverflow
Solution 5 - JavascriptOlivierHView Answer on Stackoverflow
Solution 6 - JavascriptHessamView Answer on Stackoverflow
Solution 7 - JavascriptRizwanView Answer on Stackoverflow
Solution 8 - JavascriptVictor Ribeiro da Silva EloyView Answer on Stackoverflow
Solution 9 - JavascriptKuzgunView Answer on Stackoverflow