首页 > 解决方案 > javascript remove a exact index character from the string

问题描述

let test = 'This is the test string';

console.log(test.substr(3));

console.log(test.slice(3));

console.log(test.substring(3));

Theese methods are removing first 3 character. But i want to remove only third character from the string.

The log has to be: ' Ths is the test string'

İf you help me i will be glad. All examples are giving from the substr, slice eg. eg. Are there any different methods?

标签: javascript

解决方案


First, get the first 3 chars, then add chars 4-end, connect those to get the desired result:

let test = 'This is the test string';

let res = test.substr(0, 2) + test.substr(3);

console.log(res);

Since substr uses the following parameters

substr(start, length)

Start The index of the first character to include in the returned substring.

Length Optional. The number of characters to extract.
If length is omitted, substr() extracts characters to the end of the string.

We can use test.substr(3) to get from the 3'th to the last char without specifying the length of the string


推荐阅读