javascript - 将一个词转换成另一个词
问题描述
如何将一个单词传递到另一个单词中的特定位置?假设我有一个名为“apartment”的字符串,我想在字符串“apartment”中的 3 个字母之后传递另一个名为“blue”的字符串。前任。“公寓”
解决方案
您可以创建一个可重用的函数,该函数将从3
字符中分割给定的单词并相应地添加新单词。
function replacement(word, index){
return word.slice(0,index)+'blue'+word.slice(3);
}
var res = replacement('appartment', 3);
console.log(res);
res = replacement('other', 3);
console.log(res);
您还可以使用以下substring
方法:
function replacement(word, index){
return word.substr(0,index)+'blue'+word.substr(3, word.length-1);
}
var res = replacement('appartment', 3);
console.log(res);
res = replacement('other', 3);
console.log(res);
推荐阅读
- c# - 如何使用 C# 将 JSON 转换为对象
- android - Android 在应用重启时保存包含按钮和文本框的 ListView 项的状态
- c# - BeginLifetimeScope / DbContext 的 Autofac 和内存泄漏已被处理 / C# asp.net
- ansible - 遍历 Ansibles group_vars 中的嵌套字典
- c# - WebClient OpenRead 不下载完整文件
- ios - iOS继续减速至顶
- jquery - 使用ajax jquery将图像上传到亚马逊s3
- makefile - 使用 Make 的条件语句
- angular - 如何过滤mat-tree组件Angular Material 6.0.1
- javascript - 如何使用 Express/Mongoose 将模型引用到我的用户模型