javascript - แทรกสตริงที่ตำแหน่งเฉพาะ
ฉันจะใส่สตริงหนึ่งในดัชนีเฉพาะของสตริงอื่นได้อย่างไร?
var txt1 = "foo baz"
สมมติว่าฉันต้องการใส่ "bar " หลังจาก "foo" ฉันสามารถทำอย่างนั้นได้อย่างไร?
ฉันนึกถึง substring() แต่ต้องมีวิธีที่ง่ายและตรงไปตรงมามากกว่านี้แน่นอน
คำตอบ #1
การแทรกที่ดัชนีเฉพาะ (แทนที่จะเป็น เช่น ที่ตัวอักษรเว้นวรรคตัวแรก) ต้องใช้การแบ่งสตริง/อักขระย่อย:
var txt2 = txt1.slice(0, 3) + "bar" + txt1.slice(3);
คำตอบ #2
คุณสามารถสร้างต้นแบบของ splice() ของคุณเองลงใน String ได้
โพลีฟิลล์
if (!String.prototype.splice) {
/**
* {JSDoc}
*
* The splice() method changes the content of a string by removing a range of
* characters and/or adding new characters.
*
* @this {String}
* @param {number} start Index at which to start changing the string.
* @param {number} delCount An integer indicating the number of old chars to remove.
* @param {string} newSubStr The String that is spliced in.
* @return {string} A new string with the spliced substring.
*/
String.prototype.splice = function(start, delCount, newSubStr) {
return this.slice(0, start) + newSubStr + this.slice(start + Math.abs(delCount));
};
}
ตัวอย่าง
String.prototype.splice = function(idx, rem, str) {
return this.slice(0, idx) + str + this.slice(idx + Math.abs(rem));
};
var result = "foo baz".splice(4, 0, "bar ");
document.body.innerHTML = result; // "foo bar baz"แก้ไข: ปรับแก้เพื่อให้แน่ใจว่า rem เป็นค่าที่แน่นอน
คำตอบ #3
นี่คือวิธีที่ฉันเขียนขึ้นซึ่งทำงานเหมือนกับภาษาการเขียนโปรแกรมอื่น ๆ ทั้งหมด:
String.prototype.insert = function(index, string) {
if (index > 0)
{
return this.substring(0, index) + string + this.substring(index, this.length);
}
return string + this;
};
//Example of use:
var something = "How you?";
something = something.insert(3, " are");
console.log(something)อ้างอิง:
- http://coderamblings.wordpress.com/2012/07/09/insert-a-string-at-a-specific-index/
คำตอบ #4
แค่สร้างฟังก์ชันต่อไปนี้:
function insert(str, index, value) {
return str.substr(0, index) + value + str.substr(index);
}
แล้วก็ใช้มันแบบนั้น:
alert(insert("foo baz", 4, "bar "));
ผลลัพธ์: foo bar baz
มันทำงานเหมือนกับ C# (ชาร์ป) String.Insert(int startIndex, string value) อย่างเป๊ะ
หมายเหตุ: ฟังก์ชันการแทรกนี้จะแทรกสตริง value (พารามิเตอร์ตัวที่สาม) ก่อนจำนวนเต็มที่ระบุ index (พารามิเตอร์ตัวที่สอง) ในสตริง str (พารามิเตอร์ตัวแรก) แล้วจึงส่งกลับสตริงใหม่โดยไม่เปลี่ยนแปลง str!
คำตอบ #5
- สร้างอาร์เรย์จากสตริง
- ใช้ Array#splice
- แปลงเป็นสตริงอีกครั้งโดยใช้ Array#join
ประโยชน์ของแนวทางนี้มีสองประการ:
- ง่าย
- เป็นไปตามจุดรหัส Unicode
const pair = Array.from('USDGBP')
pair.splice(3, 0, '/')
console.log(pair.join(''))คำตอบ #6
นี่คือการทำสิ่งที่ @Base33 กำลังทำอยู่โดยพื้นฐาน ยกเว้นว่าฉันยังให้ตัวเลือกในการใช้ดัชนีลบเพื่อนับจากท้าย เช่นเดียวกับวิธี substr ที่อนุญาต
// use a negative index to insert relative to the end of the string.
String.prototype.insert = function (index, string) {
var ind = index < 0 ? this.length + index : index;
return this.substring(0, ind) + string + this.substr(ind);
};
ตัวอย่าง: สมมติว่าคุณมีภาพขนาดเต็มที่ใช้รูปแบบการตั้งชื่อ แต่ไม่สามารถอัปเดตข้อมูลเพื่อให้มี URL ของรูปย่อได้
var url = '/images/myimage.jpg';
var thumb = url.insert(-4, '_thm');
// result: '/images/myimage_thm.jpg'
คำตอบ #7
อัปเดต 2016: นี่คืออีกฟังก์ชันต้นแบบ เพื่อความสนุก (แต่จริงจังมากขึ้น!) ซึ่งอิงตามวิธี 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"
ก่อนหน้า (ย้อนกลับไปปี 2012) เพื่อความสนุกสนาน วิธีแก้:
var index = 4,
what = 'bar ';
'foo baz'.replace(/./g, function(v, i) {
return i === index - 1 ? v + what : v;
}); // "foo bar baz"
คำตอบ #8
หากใครกำลังมองหาวิธีในการแทรกข้อความที่หลายดัชนีในสตริง ลองวิธีนี้ดู:
String.prototype.insertTextAtIndices = function(text) {
return this.replace(/./g, function(character, index) {
return text[index] ? text[index] + character : character;
});
};
ตัวอย่างเช่น คุณสามารถใช้สิ่งนี้เพื่อแทรกแท็ก <span> ที่ตำแหน่งเบี่ยงเบนบางตำแหน่งในสตริง:
var text = {
6: "<span>",
11: "</span>"
};
"Hello world!".insertTextAtIndices(text); // returns "Hello <span>world</span>!"
คำตอบ #9
จากตัวอย่างปัจจุบันของคุณ คุณสามารถได้ผลลัพธ์โดยวิธีใดวิธีหนึ่งดังนี้
var txt2 = txt1.split(' ').join(' bar ')
หรือ
var txt2 = txt1.replace(' ', ' bar ');
แต่เนื่องจากคุณสามารถตั้งสมมติฐานเช่นนั้นได้ คุณอาจข้ามไปที่ตัวอย่างของ Gullen โดยตรงเลยก็ได้
ในสถานการณ์ที่คุณแทบจะไม่สามารถสมมติอะไรได้เลยนอกจากการอิงดัชนีอักขระแล้ว ผมก็จะแนะนำให้ใช้วิธีการหาตัวอักษรย่อยจริงๆ
