如何按范围替换字符串? [英] How can I replace a string by range?

查看:29
本文介绍了如何按范围替换字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要按范围替换字符串示例:

I need to replace a string by range Example:

string = "this is a string";//I need to replace index 0 to 3 whith another string Ex.:"that"
result = "that is a string";

但这需要动态的.不能替换固定词...需要按范围

but this need to be dinamically. Cant be replace a fixed word ...need be by range

我试过了

           result = string.replaceAt(0, 'that');

但这仅替换第一个字符,我想要第一个到第三个

but this replace only the first character and I want the first to third

推荐答案

function replaceRange(s, start, end, substitute) {
    return s.substring(0, start) + substitute + s.substring(end);
}

var str = "this is a string";
var newString = replaceRange(str, 0, 4, "that"); // "that is a string"

这篇关于如何按范围替换字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆