जवाबों:
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वांछित दूसरे स्ट्रिंग में विभाजित करने के लिए इस्तेमाल किया जा सकता है ।indexremoveCount
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('');हूं कि मुझे "मैं एक सेब चाहिए" के बजाय ओपी को "मुझे एक सेब चाहिए"।
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'
हो सकता है कि यदि आप इंडेक्सऑफ () का उपयोग करके स्थिति का निर्धारण करते हैं तो यह और भी बेहतर है :
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");
ध्यान दें, मैंने रिटर्न कॉल के बजाय फ़ंक्शन कॉल में "a" के बाद एक स्थान रखा।
Underscore.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);
(संपादित करें: मैं देखता हूं कि यह वास्तव में ऊपर उत्तर दिया गया है, क्षमा करें!)
यदि ES2018 का लुकअप उपलब्ध है , तो एक और रीजैक्सप सॉल्यूशन, जो कि Nth कैरेक्टर (@Kamil Kiełczewski के समान) पर शून्य-चौड़ाई की स्थिति में "रिप्लेस" करने के लिए इसका उपयोग करता है , लेकिन एक कैप्चरिंग ग्रुप में प्रारंभिक वर्णों को देखे बिना:
"I want apple".replace(/(?<=^.{6})/, " an")
अच्छी तरह से सिर्फ एक छोटा सा परिवर्तन 'उपरोक्त समाधान आउटपुट का कारण बनता है
"मुझे एक सेब चाहिए"
के बजाय
"मुझे एक सेब चाहिए"
के रूप में उत्पादन प्राप्त करने के लिए
"मुझे एक सेब चाहिए"
निम्नलिखित संशोधित कोड का उपयोग करें
var output = a.substr(0, position) + " " + b + a.substr(position);