如何在javascript字符串中删除索引之间的字符 [英] how can i remove chars between indexes in a javascript string
本文介绍了如何在javascript字符串中删除索引之间的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下内容:
var S="hi how are you";
var bindex = 2;
var eindex = 6;
如何删除位于bindex和eindex之间的S中的所有字符?
因此S将是你好吗
how can i remove all the chars from S that reside between bindex and eindex?
therefore S will be "hi are you"
推荐答案
首先找到要替换的字符串的子串,然后替换第一个使用空字符串出现该字符串。
First find the substring of the string to replace, then replace the first occurrence of that string with the empty string.
S = S.replace(S.substring(bindex, eindex), "");
另一种方法是将字符串转换为数组, splice
输出不需要的部分并再次转换为字符串。
Another way is to convert the string to an array, splice
out the unwanted part and convert to string again.
var result = S.split('');
result.splice(bindex, eindex - bindex);
S = result.join('');
这篇关于如何在javascript字符串中删除索引之间的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文