[SOLVED] How to remove the first and the last character of a string

Issue

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

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.

Answered By – Dieterg

Answer Checked By – Pedro (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *