इस उदाहरण में, आप यह जाँचने के लिए जावास्क्रिप्ट प्रोग्राम लिखना सीखेंगे कि क्या एक स्ट्रिंग शुरू होती है और कुछ वर्णों के साथ समाप्त होती है।
इस उदाहरण को समझने के लिए, आपको निम्नलिखित जावास्क्रिप्ट प्रोग्रामिंग विषयों का ज्ञान होना चाहिए:
- जावास्क्रिप्ट स्ट्रिंग
- जावास्क्रिप्ट स्ट्रिंग शुरू होता है ()
- जावास्क्रिप्ट स्ट्रिंग समाप्त होता है ()
- जावास्क्रिप्ट रेगेक्स
उदाहरण 1: अंतर्निहित विधियों का उपयोग करके स्ट्रिंग की जाँच करें
// program to check if a string starts with 'S' and ends with 'G' function checkString(str) ( // check if the string starts with S and ends with G if(str.startsWith('S') && str.endsWith('G')) ( console.log('The string starts with S and ends with G'); ) else if(str.startsWith('S')) ( console.log('The string starts with S but does not end with G'); ) else if(str.endsWith('G')) ( console.log('The string starts does not with S but end with G'); ) else ( console.log('The string does not start with S and does not end with G'); ) ) // take input let string = prompt('Enter a string: '); checkString(string);
आउटपुट
स्ट्रिंग दर्ज करें: स्ट्रिंग स्ट्रिंग S से शुरू होती है लेकिन G के साथ समाप्त नहीं होती है
उपरोक्त कार्यक्रम में, दो तरीके startsWith()
और endsWith()
उपयोग किए जाते हैं।
startsWith()
विधि चेकों स्ट्रिंग विशेष स्ट्रिंग के साथ शुरू होता है।endsWith()
विधि चेकों विशेष तार के साथ समाप्त होता है स्ट्रिंग है।
उपरोक्त कार्यक्रम लोअरकेस अक्षरों के लिए जाँच नहीं करता है। इसलिए, यहां जी और जी अलग हैं।
आप यह भी देख सकते हैं कि उपरोक्त वर्ण S या s से शुरू होता है और G या g से समाप्त होता है ।
str.startsWith('S') || str.startsWith('s') && str.endsWith('G') || str.endsWith('g');
उदाहरण 2: Regex का उपयोग करके स्ट्रिंग की जाँच करें
// program to check if a string starts with 'S' and ends with 'G' function checkString(str) ( // check if the string starts with S and ends with G if( /^S/i.test(str) && /G$/i.test(str)) ( console.log('The string starts with S and ends with G'); ) else if(/^S/i.test(str)) ( console.log('The string starts with S but does not ends with G'); ) else if(/G$/i.test(str)) ( console.log('The string starts does not with S but ends with G'); ) else ( console.log('The string does not start with S and does not end with G'); ) ) // for loop to show different scenario for (let i = 0; i < 3; i++) ( // take input const string = prompt('Enter a string: '); checkString(string); )
आउटपुट
एक स्ट्रिंग दर्ज करें: स्ट्रिंग स्ट्रिंग S से शुरू होती है और G के साथ समाप्त होती है। एक स्ट्रिंग दर्ज करें: string S से शुरू होती है और G से समाप्त होती है एक स्ट्रिंग दर्ज करें: JavaScript स्ट्रिंग S से शुरू नहीं होती है और G के साथ समाप्त नहीं होती है
उपरोक्त कार्यक्रम में, एक नियमित अभिव्यक्ति (RegEx) का उपयोग इस test()
पद्धति के साथ किया जाता है ताकि यह जांचा जा सके कि स्ट्रिंग S से शुरू होती है और G के साथ समाप्त होती है ।
/^S/i
पैटर्न जांच करता है कि स्ट्रिंग है एस या रों । यहां,i
यह दर्शाता है कि स्ट्रिंग केस-असंवेदनशील है। इसलिए, एस और रों एक ही माना जाता है।/G$/i
पैटर्न जांच करता है कि स्ट्रिंग है जी या जी ।if… else… if
बयान की स्थिति की जाँच करें और उसके अनुसार परिणाम प्रदर्शित करने के लिए प्रयोग किया जाता है।for
पाश उपयोगकर्ता से अलग आदानों लेने के लिए अलग-अलग मामलों को दिखाने के लिए प्रयोग किया जाता है।