Answers:
您可以将自己的原型制作splice()
为String。
if (!String.prototype.splice) {
/**
* {JSDoc}
*
* The splice() method changes the content of a string by removing a range of
* characters and/or adding new characters.
*
* @this {String}
* @param {number} start Index at which to start changing the string.
* @param {number} delCount An integer indicating the number of old chars to remove.
* @param {string} newSubStr The String that is spliced in.
* @return {string} A new string with the spliced substring.
*/
String.prototype.splice = function(start, delCount, newSubStr) {
return this.slice(0, start) + newSubStr + this.slice(start + Math.abs(delCount));
};
}
String.prototype.splice = function(idx, rem, str) {
return this.slice(0, idx) + str + this.slice(idx + Math.abs(rem));
};
var result = "foo baz".splice(4, 0, "bar ");
document.body.innerHTML = result; // "foo bar baz"
编辑:对其进行了修改,以确保它rem
是一个绝对值。
slice
解决方案更好,更简单。(拼接是破坏性的,拼接不是破坏性的,最好避免修改“您不知道的对象”)。该解决方案绝对不应该是第一个可见的答案,即使当时可能已经有意义。
my_array[my_array.length] = item
不是my_array.push(item)
?
splice
在这种情况下,您是对的;的确字符串是不可变的。因此,我认为这splice
是一个糟糕的关键词选择。我的主要反对意见是反对任意扩展原型,除非它们是标准的polyfills。
在特定索引处插入(而不是在第一个空格字符处)必须使用字符串切片/子字符串:
var txt2 = txt1.slice(0, 3) + "bar" + txt1.slice(3);
substring
在这里很好。slice
总的来说,我更喜欢,因为它更灵活(例如,负索引"foo baz".slice(1, -2)
)。它也略短一些,值得一提。
`${txt1.slice(0,3)}bar${txt1.slice(3)}`
delete
功能;最佳答案 ...
这是我写的一种方法,其行为与所有其他编程语言类似:
String.prototype.insert = function(index, string) {
if (index > 0)
{
return this.substring(0, index) + string + this.substring(index, this.length);
}
return string + this;
};
//Example of use:
var something = "How you?";
something = something.insert(3, " are");
console.log(something)
{}
在if-else块中添加花括号。
else
是多余的。
只需执行以下功能:
function insert(str, index, value) {
return str.substr(0, index) + value + str.substr(index);
}
然后像这样使用它:
alert(insert("foo baz", 4, "bar "));
输出:foo bar baz
它的行为与C#(Sharp)String.Insert(int startIndex,string value)完全一样。
注意:此插入函数将字符串值(第三个参数)插入字符串str(第一个参数)中指定的整数索引(第二个参数)之前,然后返回新字符串而不更改str!
2016年更新:这是基于单线方法的另一个好玩的(但更严重的!)原型函数RegExp
(带有prepend support undefined
或negative index
):
/**
* Insert `what` to string at position `index`.
*/
String.prototype.insert = function(what, index) {
return index > 0
? this.replace(new RegExp('.{' + index + '}'), '$&' + what)
: what + this;
};
console.log( 'foo baz'.insert('bar ', 4) ); // "foo bar baz"
console.log( 'foo baz'.insert('bar ') ); // "bar foo baz"
上一个(回到2012年)有趣的解决方案:
var index = 4,
what = 'bar ';
'foo baz'.replace(/./g, function(v, i) {
return i === index - 1 ? v + what : v;
}); // "foo bar baz"
如果有人在寻找在字符串中的多个索引处插入文本的方法,请尝试以下方法:
String.prototype.insertTextAtIndices = function(text) {
return this.replace(/./g, function(character, index) {
return text[index] ? text[index] + character : character;
});
};
例如,您可以使用它<span>
在字符串中某些偏移处插入标签:
var text = {
6: "<span>",
11: "</span>"
};
"Hello world!".insertTextAtIndices(text); // returns "Hello <span>world</span>!"
6: "<span>"
说:在索引6处,插入文本“ <span>”。您是说要使用整数变量的值作为插入索引吗?如果是这种情况,请尝试尝试var a=6, text = {}; text[a] = "<span>";
基本上,这是@ Bass33所做的事情,除了我还可以选择使用负索引从末尾开始计数。就像substr方法允许的那样。
// use a negative index to insert relative to the end of the string.
String.prototype.insert = function (index, string) {
var ind = index < 0 ? this.length + index : index;
return this.substring(0, ind) + string + this.substring(ind, this.length);
};
用例:假设您使用命名约定拥有全尺寸图片,但无法更新数据以提供缩略图网址。
var url = '/images/myimage.jpg';
var thumb = url.insert(-4, '_thm');
// result: '/images/myimage_thm.jpg'
my_string = "hello world";
my_insert = " dear";
my_insert_location = 5;
my_string = my_string.split('');
my_string.splice( my_insert_location , 0, my_insert );
my_string = my_string.join('');
好吧,我们可以同时使用substring和slice方法。
String.prototype.customSplice = function (index, absIndex, string) {
return this.slice(0, index) + string+ this.slice(index + Math.abs(absIndex));
};
String.prototype.replaceString = function (index, string) {
if (index > 0)
return this.substring(0, index) + string + this.substring(index, this.length);
return string + this;
};
console.log('Hello Developers'.customSplice(6,0,'Stack ')) // Hello Stack Developers
console.log('Hello Developers'.replaceString(6,'Stack ')) //// Hello Stack Developers
子字符串方法的唯一问题是它不能与负索引一起使用。总是从第0位开始获取字符串索引。
function insertString(string, insertion, place) {
return string.replace(string[place] + string[place + 1], string[place] + insertion + string[place + 1])
}
所以,对你来说 insertString("foo baz", "bar", 3);
显然,这将是一种绘画,因为您每次都必须向函数提供字符串,但是目前我不知道如何使它变得更容易string.replace(insertion, place)
。这个想法仍然存在。
另一个解决方案是将字符串切成2,然后在中间插入字符串。
var str = jQuery('#selector').text();
var strlength = str.length;
strf = str.substr(0 , strlength - 5);
strb = str.substr(strlength - 5 , 5);
jQuery('#selector').html(strf + 'inserted' + strb);
您可以使用slice(0,index) + str + slice(index)
。或者,您可以为其创建方法。
String.prototype.insertAt = function(index,str){
return this.slice(0,index) + str + this.slice(index)
}
console.log("foo bar".insertAt(4,'baz ')) //foo baz bar
您可以split()
在主字符串中添加然后使用正常splice()
String.prototype.splice = function(index,del,...newStrs){
let str = this.split('');
str.splice(index,del,newStrs.join('') || '');
return str.join('');
}
var txt1 = "foo baz"
//inserting single string.
console.log(txt1.splice(4,0,"bar ")); //foo bar baz
//inserting multiple strings
console.log(txt1.splice(4,0,"bar ","bar2 ")); //foo bar bar2 baz
//removing letters
console.log(txt1.splice(1,2)) //f baz
//remving and inseting atm
console.log(txt1.splice(1,2," bar")) //f bar baz
该方法采用一个数组数组,每个数组元素代表一个splice()
。
String.prototype.splice = function(index,del,...newStrs){
let str = this.split('');
str.splice(index,del,newStrs.join('') || '');
return str.join('');
}
String.prototype.mulSplice = function(arr){
str = this
let dif = 0;
arr.forEach(x => {
x[2] === x[2] || [];
x[1] === x[1] || 0;
str = str.splice(x[0] + dif,x[1],...x[2]);
dif += x[2].join('').length - x[1];
})
return str;
}
let txt = "foo bar baz"
//Replacing the 'foo' and 'bar' with 'something1' ,'another'
console.log(txt.splice(0,3,'something'))
console.log(txt.mulSplice(
[
[0,3,["something1"]],
[4,3,["another"]]
]
))
我想分别比较使用Base33和user113716的使用子字符串的方法和使用slice的方法,以此来编写一些代码
也看看这个性能比较,子串,切片
我使用的代码创建了巨大的字符串,并将字符串“ bar”多次插入了巨大的字符串
if (!String.prototype.splice) {
/**
* {JSDoc}
*
* The splice() method changes the content of a string by removing a range of
* characters and/or adding new characters.
*
* @this {String}
* @param {number} start Index at which to start changing the string.
* @param {number} delCount An integer indicating the number of old chars to remove.
* @param {string} newSubStr The String that is spliced in.
* @return {string} A new string with the spliced substring.
*/
String.prototype.splice = function (start, delCount, newSubStr) {
return this.slice(0, start) + newSubStr + this.slice(start + Math.abs(delCount));
};
}
String.prototype.splice = function (idx, rem, str) {
return this.slice(0, idx) + str + this.slice(idx + Math.abs(rem));
};
String.prototype.insert = function (index, string) {
if (index > 0)
return this.substring(0, index) + string + this.substring(index, this.length);
return string + this;
};
function createString(size) {
var s = ""
for (var i = 0; i < size; i++) {
s += "Some String "
}
return s
}
function testSubStringPerformance(str, times) {
for (var i = 0; i < times; i++)
str.insert(4, "bar ")
}
function testSpliceStringPerformance(str, times) {
for (var i = 0; i < times; i++)
str.splice(4, 0, "bar ")
}
function doTests(repeatMax, sSizeMax) {
n = 1000
sSize = 1000
for (var i = 1; i <= repeatMax; i++) {
var repeatTimes = n * (10 * i)
for (var j = 1; j <= sSizeMax; j++) {
var actualStringSize = sSize * (10 * j)
var s1 = createString(actualStringSize)
var s2 = createString(actualStringSize)
var start = performance.now()
testSubStringPerformance(s1, repeatTimes)
var end = performance.now()
var subStrPerf = end - start
start = performance.now()
testSpliceStringPerformance(s2, repeatTimes)
end = performance.now()
var splicePerf = end - start
console.log(
"string size =", "Some String ".length * actualStringSize, "\n",
"repeat count = ", repeatTimes, "\n",
"splice performance = ", splicePerf, "\n",
"substring performance = ", subStrPerf, "\n",
"difference = ", splicePerf - subStrPerf // + = splice is faster, - = subStr is faster
)
}
}
}
doTests(1, 100)
性能上的一般差异充其量是微不足道的,并且两种方法都可以正常工作(即使在长度约为〜12000000的字符串上)
这种方法的好处有两个:
const pair = Array.from('USDGBP')
pair.splice(3, 0, '/')
console.log(pair.join(''))