How to Remove a Character from a String Using JavaScript

How to remove text from a string?

var ret = "data-123".replace('data-','');console.log(ret);   //prints: 123

Remove a character at a certain position in a string - javascript

It depends how easy you find the following, which uses simple String methods (in this case slice()).

var str = "Hello World";str = str.slice(0, 3) + str.slice(4);console.log(str)

How to remove one character based on index in javascript?

Instead of using .replace() you'd just concatenate slices of the string before and after the current index.

var str = 'batman';
for (var i = 0; i < str.length; i++) { var minusOneStr = str.slice(0, i) + str.slice(i + 1); console.log(minusOneStr);}

How to remove ↵ character from JS string

Instead of using the literal character, you can use the codepoint.

Instead of "↵" you can use "\u21b5".

To find the code, use '<character>'.charCodeAt(0).toString(16), and then use like \u<number>.

Now, you can use it like this:

string.split("\u21b5").join(''); //split+join

string.replace(/\u21b5/g,''); //RegExp with unicode point

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


Related Topics



Leave a reply



Submit