在另一个字符串的位置x处插入字符串

sam*_*ami 218 javascript

我有2个vars,需要在这个位置插入b.我正在寻找的结果是"我想要一个苹果".我怎么能用jquery或javascript做到这一点?

var a = 'I want apple';
var b = ' an';
var position = 6;
Run Code Online (Sandbox Code Playgroud)

jAn*_*ndy 352

var a = "I want apple";
var b = "an";
var position = 6;
var output = [a.slice(0, position), b, a.slice(position)].join('');
console.log(output);
Run Code Online (Sandbox Code Playgroud)

  • 这个解决方案并不快.我对此很好奇并且运行了一个jsperf.这是对将来阅读此内容的任何人的说明.http://jsperf.com/javascript-string-splice.测试了最新的FF/Chrome/IE10/IE9.为了清晰度和性能,我会使用精益切口的方法. (34认同)
  • @PaulVon纠正一些事情永远不会错,所以不需要原谅.无论如何,我有点不同意.功能上做了它想做的事情,在另一个字符串中的某个位置插入一个字符串.实际上插入的字符串应该像"an",在这种情况下更正确. (12认同)
  • 对于长字符串,此解决方案比nickf的解决方案更快(因为它复制更少). (3认同)
  • 嗯,这很有可能.这里的答案已经快3年了,当时的大多数浏览器和版本确实使用*Array*join(尤其是IE)表现得更快. (3认同)

nic*_*ckf 237

var output = a.substring(0, position) + b + a.substring(position);
Run Code Online (Sandbox Code Playgroud)

  • 根据@junkyspace https://jsperf.com/javascript-string-splice,这个答案比jAndy的答案快**640倍. (44认同)
  • 现在不建议使用String.prototype.substr。https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String/substr (5认同)
  • [`substring`](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String/substring)*在这种情况下*是已弃用的`substr`的​​直接替代,因此答案是:`var output = a.substring(0,position)+ b + a.substring(position);`。 (2认同)

jas*_*_89 30

您可以将此函数添加到字符串类

String.prototype.insert_at=function(index, string)
{   
  return this.substr(0, index) + string + this.substr(index);
}
Run Code Online (Sandbox Code Playgroud)

这样你就可以在任何字符串对象上使用它:

var my_string = "abcd";
my_string.insertAt(1, "XX");
Run Code Online (Sandbox Code Playgroud)

  • 修改本地对象原型是一个坏习惯:/sf/ask/982392631/ (3认同)

pau*_*o62 9

也许如果你使用indexOf()确定位置会更好:

function insertString(a, b, at)
{
    var position = a.indexOf(at); 

    if (position !== -1)
    {
        return a.substr(0, position) + b + a.substr(position);    
    }  

    return "substring not found";
}
Run Code Online (Sandbox Code Playgroud)

然后像这样调用函数:

insertString("I want apple", "an ", "apple");
Run Code Online (Sandbox Code Playgroud)

注意,我在函数调用中的"an"后面放了一个空格,而不是在return语句中.

  • 这不是要求的内容。即使是这种情况,如果您多次出现“ at”子字符串,也将无法使用 (2认同)

Ste*_*n J 6

使用ES6字符串文字,将简短得多:

const insertAt = (str, sub, pos) => `${str.slice(0, pos)}${sub}${str.slice(pos)}`;
    
console.log(insertAt('I want apple', ' an', 6)) // logs 'I want an apple'
Run Code Online (Sandbox Code Playgroud)


sva*_*rog 5

Underscore.String图书馆,做了功能插入

insert(string,index,substring)=>字符串

像这样

insert("Hello ", 6, "world");
// => "Hello world"
Run Code Online (Sandbox Code Playgroud)

  • 尽管我不喜欢使用库,除非有必要,但我赞成抵消反对票:P (2认同)

Kam*_*ski 5

尝试

a.slice(0,position) + b + a.slice(position)
Run Code Online (Sandbox Code Playgroud)

a.slice(0,position) + b + a.slice(position)
Run Code Online (Sandbox Code Playgroud)

或正则表达式解决方案

"I want apple".replace(/^(.{6})/,"$1 an")
Run Code Online (Sandbox Code Playgroud)

var a = "I want apple";
var b = " an";
var position = 6;

var r= a.slice(0,position) + b + a.slice(position);

console.log(r);
Run Code Online (Sandbox Code Playgroud)


dla*_*zon 5

如果 ES2018 的lookbehind可用,还有一个正则表达式解决方案,它利用它在第 N 个字符之后的零宽度位置“替换” (类似于@Kamil Kie?czewski,但不将初始字符存储在捕获组中) :

"I want apple".replace(/(?<=^.{6})/, " an")
Run Code Online (Sandbox Code Playgroud)

"I want apple".replace(/(?<=^.{6})/, " an")
Run Code Online (Sandbox Code Playgroud)