কম্পিউটার টিউটোরিয়াল

জাভাস্ক্রিপ্টে শব্দ ভাঙা ছাড়া ফিক্সড দৈর্ঘ্যের ব্লকে সেন্টেন্স ভাগ করা


আমাদের একটি জাভাস্ক্রিপ্ট ফাংশন লিখতে হবে যা একটি স্ট্রিং প্রথম আর্গুমেন্ট হিসেবে নেয় যাতে একটি প্যারাগ্রাফের টেক্সট থাকে এবং একটি চাঙ্ক সাইজ সংখ্যা দ্বিতীয় আর্গুমেন্ট হিসেবে।

ফাংশনটি নিচের কাজগুলো করবে −

  • স্ট্রিংকে চাঙ্ক সাইজ (দ্বিতীয় আর্গুমেন্ট) এর চেয়ে বড় না হয় এমন চাঙ্কে ভাগ করবে,

  • ভাগ করা শুধু হোয়াইটস্পেস বা সেন্টেন্স এন্ডে হবে (শব্দ ভাঙবে না)।

উদাহরণস্বরূপ − ইনপুট স্ট্রিং যদি হয় −

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 &minu; 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. জাভাস্ক্রিপ্টে ম্যাট্রিক্সে শব্দ খুঁজে বের করা

  2. জাভাস্ক্রিপ্টে একটি সেন্টেন্স থেকে n সবচেয়ে বেশি ফ్రీকোয়েন্ট শব্দ বের করা

  3. জাভাস্ক্রিপ্টে camelCase স্ট্রিং ভাঙন

  4. C++-তে সেন্টেন্সকে ওয়ার্ডে ভাগ করা