Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> JavaScript

जावास्क्रिप्ट में वाक्य को शब्दों को तोड़े बिना निश्चित लंबाई के ब्लॉकों में विभाजित करना


हमें एक जावास्क्रिप्ट फ़ंक्शन लिखना है जो पहली तर्क के रूप में एक पैराग्राफ का टेक्स्ट वाली स्ट्रिंग और दूसरी तर्क के रूप में एक चंक आकार संख्या लेता है।

फ़ंक्शन को निम्नलिखित चीजें करनी चाहिए -

  • स्ट्रिंग को चंक आकार (दूसरी तर्क) से अधिक नहीं की लंबाई के टुकड़ों में तोड़ना,
  • टूटना केवल व्हाइटस्पेस या वाक्य के अंत पर होना चाहिए (शब्द को नहीं तोड़ना चाहिए)।

उदाहरण के लिए - यदि इनपुट स्ट्रिंग है -

const str = 'this is a string'; 
const chunkLength = 6;

तो आउटपुट होना चाहिए -

const output = ['this', 'is a', 'string'];

आइए इस फ़ंक्शन के लिए कोड लिखें -

हम निर्दिष्ट संख्या में अक्षरों से मिलान करने के लिए एक नियमित अभिव्यक्ति का उपयोग करेंगे। एक बार मिलान हो जाने पर हम तब तक बैकट्रैक करेंगे जब तक हमें व्हाइटस्पेस या स्ट्रिंग का अंत नहीं मिल जाता।

उदाहरण

इसके लिए कोड होगा -

const size = 200; 
const str = "This process was continued for several years for the deaf child does not here in a month or even in two or three years the numberless items and expressions using the simplest daily intercourse little hearing child learns from these constant rotation and imitation the conversation he hears in his home simulates is mine and suggest topics and called forth the spontaneous expression of his own thoughts."; 
const splitString = (str = '', size) => { 
  const regex = new RegExp(String.raw`\S.{1,${size - 2}}\S(?= |$)`, 'g'); 
  const chunks = str.match(regex); 
  return chunks; 
} 
console.log(splitString(str, size));

आउटपुट

कंसोल में आउटपुट होगा -

[ 
  'This process was continued for several years for the deaf child does not here in a month or even in two or three years the numberless items and expressions using the simplest daily intercourse little', 
  'hearing child learns from these constant rotation and imitation the conversation he hears in his home simulates is mine and suggest topics and called forth the spontaneous expression of his own', 
  'thoughts.' 
]
  1. - JavaScript में वाक्य से n सबसे अधिक बार आने वाले शब्द ढूंढना

    इस प्रश्न के उद्देश्य के लिए, हम एक वाक्य को एक स्ट्रिंग के रूप में परिभाषित करते हैं जिसमें अंग्रेजी वर्णमाला और विराम चिह्न होते हैं, और एक शब्द उस वाक्य का उपस्ट्रिंग होता है जो व्हाइटस्पेस से जुड़ा होता है।हमें एक JavaScript फ़ंक्शन लिखना है जो पहली आर्ग्युमेंट के रूप में वाक्य स्ट्रिंग str और द

  1. - JavaScript में कैमलकेस सिंटैक्स को तोड़ना

    समस्या हमें एक JavaScript फ़ंक्शन लिखना है जो एक कैमलकेस स्ट्रिंग, str, को पहला और एकमात्र तर्क के रूप में लेता है। हमारे फ़ंक्शन को एक नई स्ट्रिंग बनाकर वापस करनी चाहिए जो इनपुट स्ट्रिंग को शब्दों के बीच स्पेस का उपयोग करके विभाजित करती है। उदाहरण के लिए, यदि फ़ंक्शन का इनपुट है - इनपुट const str =

  1. - C++ में वाक्य को शब्दों में विभाजित करना

    दिया गया कार्य वाक्य को शब्दों में विभाजित करना है। इसमें, हम वाक्य में मौजूद सभी शब्दों को अलग करेंगे। इनपुट I am a good boy आउटपुट I am a good boy उपरोक्त उदाहरण में हम प्रत्येक शब्द को अलग लाइन में प्रिंट करेंगे। उदाहरण #include <iostream> #include <string> #include <sstream>