मान लीजिए निम्नलिखित हमारा कॉमा और व्हाइटस्पेस वाला स्ट्रिंग है:
var sentences = " John , David , Bob , Mike, Carol ";
कॉमा से वाक्यों को विभाजित करने के लिए split() का उपयोग करें। आसपास के स्पेस हटाने के लिए trim() का उपयोग करें।
उदाहरण
निम्नलिखित कोड देखें:
var sentences = " John , David , Bob , Mike, Carol ";
console.log("The value=" + sentences);
var result = sentences.split(",").map(function (value) {
return value.trim();
});
console.log("After modifying the value=")
console.log(result);
उपरोक्त प्रोग्राम चलाने के लिए, निम्नलिखित कमांड का उपयोग करें:
node fileName.js.
यहां, मेरा फ़ाइल नाम demo235.js है।
आउटपुट
आउटपुट इस प्रकार है:
PS C:\Users\Amit\javascript-code> node demo235.js The value= John , David , Bob , Mike, Carol After modifying the value= [ 'John', 'David', 'Bob', 'Mike', 'Carol' ]