本文翻译自:Insert a string at a specific index
How can I insert a string at a specific index of another string? 如何在另一个字符串的特定索引处插入一个字符串?
var txt1 = "foo baz"
Suppose I want to insert "bar " after the "foo" how can I achieve that? 假设我要在“ foo”之后插入“ bar”,我该如何实现?
I thought of substring()
, but there must be a simpler more straight forward way. 我想到了substring()
,但是必须有一个更简单,更直接的方法。
#1楼
参考:https://stackoom.com/question/I6E5/在特定索引处插入字符串
#2楼
UPDATE 2016: Here is another just-for-fun (but more serious!) prototype function based on one-liner RegExp
approach (with prepend support on undefined
or negative index
): 2016年更新:这是基于单行RegExp
方法的另一个有趣的原型函数(但更严重!)(具有对undefined
或负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"
Previous (back to 2012) just-for-fun solution: 上一个(回到2012年) 有趣的解决方案:
var index = 4,
what = 'bar ';
'foo baz'.replace(/./g, function(v, i) {
return i === index - 1 ? v + what : v;
}); // "foo bar baz"
#3楼
another solution, cut the string in 2 and put a string in between. 另一个解决方案是,将字符串切成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);
#4楼
You can use Regular Expressions with a dynamic pattern. 您可以将正则表达式与动态模式一起使用。
var text = "something";
var output = " ";
var pattern = new RegExp("^\\s{"+text.length+"}");
var output.replace(pattern,text);
outputs: 输出:
"something "
This replaces text.length
of whitespace characters at the beginning of the string output
. 这将替换字符串output
开头的text.length
个空白字符。 The RegExp
means ^\\
- beginning of a line \\s
any white space character, repeated {n}
times, in this case text.length
. RegExp
表示^\\
-行\\s
任何空白字符的开头,重复{n}
次,在此情况下为text.length
。 Use \\\\
to \\
escape backslashes when building this kind of patterns out of strings. 使用\\\\
以\\
建立这种模式出来的字符串时逃生反斜线。
#5楼
Just make the following function: 只需执行以下功能:
function insert(str, index, value) {
return str.substr(0, index) + value + str.substr(index);
}
and then use it like that: 然后像这样使用它:
alert(insert("foo baz", 4, "bar "));
Output: foo bar baz 输出:foo bar baz
It behaves exactly, like the C# (Sharp) String.Insert(int startIndex, string value). 它的行为与C#(Sharp)String.Insert(int startIndex,string value)完全一样。
NOTE: This insert function inserts the string value (third parameter) before the specified integer index (second parameter) in the string str (first parameter), and then returns the new string without changing str ! 注意:此插入函数将字符串值 (第三个参数)插入到字符串str (第一个参数)中指定的整数索引 (第二个参数) 之前 ,然后返回新字符串而不更改str !
#6楼
If anyone is looking for a way to insert text at multiple indices in a string, try this out: 如果有人在寻找在字符串中的多个索引处插入文本的方法,请尝试以下方法:
String.prototype.insertTextAtIndices = function(text) {
return this.replace(/./g, function(character, index) {
return text[index] ? text[index] + character : character;
});
};
For example, you can use this to insert <span>
tags at certain offsets in a string: 例如,您可以使用它在字符串中的某些偏移处插入<span>
标记:
var text = {
6: "<span>",
11: "</span>"
};
"Hello world!".insertTextAtIndices(text); // returns "Hello <span>world</span>!"