失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > 在特定索引处插入字符串

在特定索引处插入字符串

时间:2021-08-01 19:51:36

相关推荐

在特定索引处插入字符串

本文翻译自: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 ofsubstring(), but there must be a simpler more straight forward way.我想到了substring(),但是必须有一个更简单,更直接的方法。

#1楼

参考:/question/I6E5/在特定索引处插入字符串

#2楼

UPDATE :Here is anotherjust-for-fun(but more serious!) prototype function based on one-linerRegExpapproach (with prepend support onundefinedor negativeindex):更新:这是基于单行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 )just-for-funsolution:上一个(回到)有趣的解决方案:

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 adynamicpattern.您可以将正则表达式与动态模式一起使用。

var text = "something";var output = "";var pattern = new RegExp("^\\s{"+text.length+"}");var output.replace(pattern,text);

outputs:输出:

"something"

This replacestext.lengthof whitespace characters at the beginning of the stringoutput.这将替换字符串output开头的text.length个空白字符。TheRegExpmeans^\\- beginning of a line\\sany white space character, repeated{n}times, in this casetext.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 stringvalue(third parameter)beforethe specified integerindex(second parameter) in the stringstr(first parameter), and then returns the new string without changingstr!注意:此插入函数将字符串(第三个参数)插入到字符串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>!"

如果觉得《在特定索引处插入字符串》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。