ฉันมีสองตัวแปรและจำเป็นที่จะต้องสตริงแทรกbเป็นสตริงที่จุดที่แสดงโดยa positionผลลัพธ์ที่ฉันต้องการคือ "ฉันต้องการแอปเปิ้ล" ฉันจะทำสิ่งนี้กับ JavaScript ได้อย่างไร
var a = 'I want apple';
var b = ' an';
var position = 6;
ฉันมีสองตัวแปรและจำเป็นที่จะต้องสตริงแทรกbเป็นสตริงที่จุดที่แสดงโดยa positionผลลัพธ์ที่ฉันต้องการคือ "ฉันต้องการแอปเปิ้ล" ฉันจะทำสิ่งนี้กับ 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);
ต่อไปนี้สามารถใช้เพื่อประกบกันtextภายในสตริงอื่นที่ต้องการindexพร้อมกับremoveCountพารามิเตอร์เสริม
if (String.prototype.splice === undefined) {
/**
* Splices text within a string.
* @param {int} offset The position to insert the text at (before)
* @param {string} text The text to insert
* @param {int} [removeCount=0] An optional number of characters to overwrite
* @returns {string} A modified string containing the spliced text.
*/
String.prototype.splice = function(offset, text, removeCount=0) {
let calculatedOffset = offset < 0 ? this.length + offset : offset;
return this.substring(0, calculatedOffset) +
text + this.substring(calculatedOffset + removeCount);
};
}
let originalText = "I want apple";
// Positive offset
console.log(originalText.splice(6, " an"));
// Negative index
console.log(originalText.splice(-5, "an "));
// Chaining
console.log(originalText.splice(6, " an").splice(2, "need", 4).splice(0, "You", 1));
.as-console-wrapper { top: 0; max-height: 100% !important; }
var output = [a.slice(0, position + 1), b, a.slice(position)].join('');แก่การให้ OPs "ฉันต้องการแอปเปิ้ล" แทนที่จะเป็น "I wantan apple"
var output = a.substring(0, position) + b + a.substring(position);
แก้ไข: แทนที่.substrด้วย.substringเพราะ.substrตอนนี้เป็นฟังก์ชันดั้งเดิม (ต่อhttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substr )
String.prototype.substrเลิกใช้แล้วในขณะนี้ developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/
.substring
คุณสามารถเพิ่มฟังก์ชั่นนี้ในคลาสสตริง
String.prototype.insert_at=function(index, string)
{
return this.substr(0, index) + string + this.substr(index);
}
เพื่อให้คุณสามารถใช้กับวัตถุสตริงใด ๆ :
var my_string = "abcd";
my_string.insertAt(1, "XX");
การใช้ตัวอักษรสตริง 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'
อาจจะดีกว่านี้หากคุณระบุตำแหน่งโดยใช้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";
}
จากนั้นเรียกใช้ฟังก์ชันดังนี้:
insertString("I want apple", "an ", "apple");
โปรดทราบว่าฉันใส่ช่องว่างหลังจาก "an" ในการเรียกใช้ฟังก์ชั่นมากกว่าในคำสั่งส่งคืน
Underscore.Stringห้องสมุดมีฟังก์ชั่นที่ไม่แทรก
insert (string, index, substring) => string
ชอบมาก
insert("Hello ", 6, "world");
// => "Hello world"
ลอง
a.slice(0,position) + b + a.slice(position)
หรือโซลูชัน regexp
"I want apple".replace(/^(.{6})/,"$1 an")
var array = a.split(' ');
array.splice(position, 0, b);
var output = array.join(' ');
สิ่งนี้จะช้าลง แต่จะดูแลการเพิ่มพื้นที่ก่อนและหลังการนอกจากนี้คุณจะต้องเปลี่ยนค่าของตำแหน่ง (เป็น 2 ซึ่งตอนนี้ใช้งานได้ง่ายขึ้น)
แก้ไขด่วน! หากคุณไม่ต้องการเพิ่มช่องว่างด้วยตนเองคุณสามารถทำได้:
var a = "I want apple";
var b = "an";
var position = 6;
var output = [a.slice(0, position + 1), b, a.slice(position)].join('');
console.log(output);
(แก้ไข: ฉันเห็นว่านี่เป็นคำตอบจริงข้างต้นขอโทษ!)
ถ้า lookbehind ของ ES2018 มีอีกหนึ่งโซลูชัน regexp ที่ใช้เพื่อ "แทนที่" ที่ตำแหน่งศูนย์กว้างหลังจากอักขระ Nth (คล้ายกับ @Kamil Kiełczewski แต่ไม่มีการเก็บอักขระเริ่มต้นในกลุ่มการจับ):
"I want apple".replace(/(?<=^.{6})/, " an")
การเปลี่ยนแปลงเพียงเล็กน้อยก็เป็นสาเหตุให้โซลูชันดังกล่าวส่งออก
"ฉันต้องการ anapple"
แทน
"ฉันต้องการแอปเปิ้ล"
เพื่อรับเอาท์พุทเป็น
"ฉันต้องการแอปเปิ้ล"
ใช้รหัสแก้ไขต่อไปนี้
var output = a.substr(0, position) + " " + b + a.substr(position);