在另一个字符串的位置x处插入字符串 [英] Inserting string at position x of another string

查看:118
本文介绍了在另一个字符串的位置x处插入字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个vars,需要在此位置插入b。我正在寻找的结果是我想要一个苹果。如何使用jquery或javascript执行此操作?

I have 2 vars and need to insert b into a at this position. The result I'm looking for is "I want an apple". How can I do this with jquery or javascript?

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


推荐答案

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);

这篇关于在另一个字符串的位置x处插入字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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