JS स्ट्रिंग मेथड्स
In this page:
string.length;
string.slice(start, end);
string.replace(old, new);
string.split(separator);
string.toUpperCase();
Case बदलना
toUpperCase() स्ट्रिंग के हर letter को uppercase में बदल देता है, और toLowerCase() हर letter को lowercase में बदल देता है, दोनों ही original को बदलने के बजाय एक बिल्कुल नई स्ट्रिंग return करते हैं।
उदाहरण: Changing Case
// 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 आ सकते हैं।
उदाहरण: trim
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 मान लेता है।
उदाहरण: slice and substring
// 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 को बदल देता है।
उदाहरण: replace and replaceAll
// 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 की मौजूदगी जांचते हैं।
उदाहरण: split, includes, startsWith, and endsWith
// 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"));
- यह भूल जाना कि string methods original को modify करने के बजाय एक बिल्कुल नई string return करते हैं, JavaScript में strings immutable हैं।
- slice() और substring() को confuse करना, वे similarly व्यवहार करते हैं लेकिन negative indexes को अलग तरीके से handle करते हैं।
- 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: