← Back to JavaScript Course | Chapter 3: Strings, Arrays & Data | Lesson 2 of 6

JS स्ट्रिंग मेथड्स

एक text editor की toolbar के बारे में सोचें, uppercase के लिए बटन, extra spaces trim करने के लिए, शब्द ढूँढने और बदलने के लिए, हर एक बटन आपके टेक्स्ट को बदलने का एक खास, well-defined काम करता है। JavaScript स्ट्रिंग्स के साथ बिल्कुल वैसी ही built-in methods की पूरी toolbar आती है, जो आपको case बदलने, स्ट्रिंग के हिस्से काटने, खास शब्द खोजने, या किसी वाक्य को अलग-अलग टुकड़ों में split करने देती है, वो भी खुद वह logic लिखे बिना। ये methods असली applications में लगातार इस्तेमाल होती हैं, cookiescursor.com पर किसी tutorial title को format करने से लेकर किसी visitor के टाइप किए email address को validate करने तक।
Syntax
javascript
string.length;
string.slice(start, end);
string.replace(old, new);
string.split(separator);
string.toUpperCase();

Case बदलना

toUpperCase() स्ट्रिंग के हर letter को uppercase में बदल देता है, और toLowerCase() हर letter को lowercase में बदल देता है, दोनों ही original को बदलने के बजाय एक बिल्कुल नई स्ट्रिंग return करते हैं।

Note: किसी comparison के दोनों पक्षों को toLowerCase() से एक जैसे case में बदलना, case-insensitive matching के लिए एक आम trick है।
Warning: चूंकि स्ट्रिंग्स immutable होती हैं, toUpperCase() को बिना उसका result store किए कॉल करने से कुछ फायदा नहीं होता, आपको return हुई value को capture करना ही होगा।

उदाहरण: Changing Case

javascript
// Declare the constant `text`, set to "Hello World"
// Declare the constant `text`, set to "Hello World"
const text = "Hello World";
// Print `text.toUpperCase()` to the console
// Print `text.toUpperCase()` to the console
console.log(text.toUpperCase());
// Print `text.toLowerCase()` to the console
// Print `text.toLowerCase()` to the console
console.log(text.toLowerCase());

trim

trim() स्ट्रिंग की शुरुआत और अंत से whitespace हटा देता है, जो खासतौर पर user के टाइप किए हुए input को साफ करने में उपयोगी है, जिसमें गलती से leading या trailing spaces आ सकते हैं।

Note: किसी form field से आए user input को validate करने से पहले हमेशा trim करें, visitor से बिना ध्यान दिए extra spaces टाइप हो जाना आम बात है।
Warning: trim() सिर्फ स्ट्रिंग के किनारों से whitespace हटाता है, टेक्स्ट के बीच में मौजूद spaces वैसे ही रह जाते हैं।

उदाहरण: trim

javascript
const input = "   hello   ";
console.log(`"${input.trim()}"`);

slice और substring

slice(start, end) और substring(start, end) दोनों ही index positions के आधार पर स्ट्रिंग का एक हिस्सा निकालते हैं, slice अंत से गिनते हुए negative indexes को support करता है, जबकि substring negative values को zero मान लेता है।

Note: ज़्यादातर मामलों में, slice() ज़्यादा flexible और predictable विकल्प है, खासकर जब आप स्ट्रिंग के अंत के relative positions पर काम कर रहे हों।
Warning: दोनों methods end index तक निकालते हैं, पर उसे शामिल नहीं करते, इस off-by-one detail को भूल जाना bugs का एक आम कारण है।

उदाहरण: slice and substring

javascript
// Declare the constant `text`, set to "JavaScript"
// Declare the constant `text`, set to "JavaScript"
const text = "JavaScript";
// Print `text.slice(0, 4)` to the console
// Print `text.slice(0, 4)` to the console
console.log(text.slice(0, 4));
// Print `text.slice(-6)` to the console
// Print `text.slice(-6)` to the console
console.log(text.slice(-6));
// Print `text.substring(0, 4)` to the console
// Print `text.substring(0, 4)` to the console
console.log(text.substring(0, 4));

replace और replaceAll

replace() किसी दिए गए text या pattern के पहले match को नए text से बदल देता है, जबकि replaceAll() पूरी स्ट्रिंग में हर matching occurrence को बदल देता है।

Note: अगर आप जानबूझकर सिर्फ एक खास occurrence बदलना चाहते हैं, तो replace() सही है, वरना पूरी तरह से बदलने के लिए replaceAll() का इस्तेमाल करें।
Warning: जब असल में हर occurrence बदलना ज़रूरी हो तब replace() इस्तेमाल करना एक आम bug है, क्योंकि सिर्फ पहला match ही update होता है।

उदाहरण: replace and replaceAll

javascript
// Declare the constant `text`, set to "cat cat cat"
// Declare the constant `text`, set to "cat cat cat"
const text = "cat cat cat";
// Print `text.replace("cat", "dog")` to the console
// Print `text.replace("cat", "dog")` to the console
console.log(text.replace("cat", "dog"));
// Print `text.replaceAll("cat", "dog")` to the console
// Print `text.replaceAll("cat", "dog")` to the console
console.log(text.replaceAll("cat", "dog"));

split, includes, startsWith, और endsWith

split() किसी separator के आधार पर स्ट्रिंग को टुकड़ों की एक array में तोड़ देता है, जबकि includes(), startsWith(), और endsWith() हर एक स्ट्रिंग में किसी भी position, शुरुआत, या अंत में खास text की मौजूदगी जांचते हैं।

Note: split() खासतौर पर किसी comma-separated list, जैसे tags या categories, को एक usable array में बदलने के लिए काम आता है।
Warning: includes(), startsWith(), और endsWith() सभी डिफ़ॉल्ट रूप से case sensitive होते हैं, अगर आपकी जांच के लिए यह मायने रखता है तो पहले casing को normalize कर लें।

उदाहरण: split, includes, startsWith, and endsWith

javascript
// Declare the constant `tags`, set to "html,css,js"
// Declare the constant `tags`, set to "html,css,js"
const tags = "html,css,js";
// Print `tags.split(",")` to the console
// Print `tags.split(",")` to the console
console.log(tags.split(","));
// Print `tags.includes("css")` to the console
// Print `tags.includes("css")` to the console
console.log(tags.includes("css"));
// Print `tags.startsWith("html")` to the console
// Print `tags.startsWith("html")` to the console
console.log(tags.startsWith("html"));
// Print `tags.endsWith("js")` to the console
// Print `tags.endsWith("js")` to the console
console.log(tags.endsWith("js"));
Live Example
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह भूल जाना कि string methods original को modify करने के बजाय एक बिल्कुल नई string return करते हैं, JavaScript में strings immutable हैं।
  2. slice() और substring() को confuse करना, वे similarly व्यवहार करते हैं लेकिन negative indexes को अलग तरीके से handle करते हैं।
  3. replace() इस्तेमाल करना जब आप replaceAll() चाहते थे, replace() डिफ़ॉल्ट रूप से सिर्फ पहली matching occurrence बदलता है।
चैप्टर सारांश
  • toUpperCase, toLowerCase, और trim किसी string की casing adjust करते हैं या आस-पास का whitespace हटाते हैं।
  • slice, substring, split, और includes/startsWith/endsWith आपको किसी string के अंदर extract या search करने देते हैं।
  • replace और replaceAll matched text बदलते हैं, replaceAll हर occurrence को affect करता है।
ब्राउज़र सपोर्ट

सभी core string methods, replaceAll समेत, हर current browser version में supported हैं।

🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.