JavaScript String and Events: string types; string functions (concat(), split(), indexOf(), lastIndexOf(), substring(), trim(), slice(), replace(), charAt())

JavaScript strings immutable text हैं, तो हर string method original बदलने के बजाय एक NEW string return करता है, और toolkit joining (concat), splitting (split), searching (indexOf/lastIndexOf, जो न मिलने पर -1 return करते हैं), extracting (slice, substring, charAt), और cleaning (trim, replace) cover करता है।

10 min read · 9 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

' aditi ' 'Aditi' नहीं है

एक student FestConnect में अपना नाम " aditi " type करता है, stray spaces और lowercase के साथ। आपको एक साफ़ "Aditi" चाहिए, आप "Aditi Shah" को first और last में split करना चाहते हैं, और आप confirm करना चाहते हैं email में असल में एक @ है।

यह सब string काम है। JavaScript strings के साथ methods की एक toolkit आती है, पर वे एक rule share करती हैं जो सबको trip करती है: एक string कभी in place नहीं बदली जा सकती। हर method आपको एक नई string देता है।

Formula

Strings immutable हैं

आप एक string edit नहीं कर सकते; आप इससे सिर्फ़ एक नई बना सकते हैं।

name.trim() name को साफ़ नहीं करता। यह एक trimmed copy return करता है और name को छूता नहीं है। आपको इसे capture करना ज़रूरी है:

name = name.trim();

Assignment भूलिए और लगता है method ने 'कुछ नहीं किया'। यह अकेला rule ज़्यादातर string bugs explain करता है। नीचे का हर method एक value return करता है जिसे आपको इस्तेमाल करना ज़रूरी है।

At a glance

String toolkit

Methodकरता हैNote
trim()किनारे के spaces हटाता हैएक copy return करता है
split(sep)एक array में तोड़ता है"a b".split(" ")
indexOf(x)x की पहली positionन मिलने पर -1
lastIndexOf(x)x की आख़िरी positionन मिलने पर -1
slice(a,b)chars a..b-1negatives allow करता है
charAt(i)i पर characterrange से बाहर होने पर ""
replace(a,b)पहला match swap करता हैसबके लिए replaceAll

Practical

FestConnect एक name साफ़ करता है और एक email check करता है

let raw = "  aditi shah ";
let name = raw.trim();          // "aditi shah" (captured!)
console.log(raw);               // still "  aditi shah " (unchanged)

let parts = name.split(" ");    // ["aditi", "shah"]
console.log(parts[0], parts[1]);

let email = "aditi@x.com";
console.log(email.indexOf("@"));   // 5  (found)
console.log(email.indexOf("?"));   // -1 (not found)

console.log(email.slice(0, 5));    // "aditi"
console.log(email.charAt(0));      // "a"
console.log("a b c".replace(" ", "_")); // "a_b c" (first only)

This example runs in Gri-Learn on the web, where you can edit it and see the output.

Think first

Name dirty क्यों रहा?

एक student अपनी line पर अकेले name.trim(); लिखता है, फिर name submit करता है और इसमें अभी भी spaces हैं। वे क्या भूले, और corrected line क्या है?

Show the answer

वे result capture करना भूल गए। name.trim() एक साफ़ copy return करता है पर name को modify नहीं करता, क्योंकि strings immutable हैं। Fix है इसे वापस assign करना: name = name.trim();। यह एक habit, याद रखना कि string methods एक नई value return करते हैं जिसे आपको store करना ज़रूरी है, पूरी language का सबसे common string bug रोकता है।

Quiz

आप एक email में @ है या नहीं देखने के लिए `if (email.indexOf("@") )` check करते हैं। यह ग़लत क्यों हो सकता है?

  1. अगर @ position 0 पर है, indexOf 0 return करता है, जो falsy है, तो एक valid case reject हो जाता है
  2. indexOf true या false return करता है, कोई number नहीं
  3. indexOf @ symbol search नहीं कर सकता
  4. कुछ भी ग़लत नहीं है; यह हमेशा correctly काम करता है
Show the answer

अगर @ position 0 पर है, indexOf 0 return करता है, जो falsy है, तो एक valid case reject हो जाता है

indexOf एक position number return करता है, या न होने पर -1, कोई boolean नहीं। Position 0 एक असली match है पर falsy है, तो if (indexOf(...)) शुरुआत में एक @ को 'not found' समझता है (A)। साथ ही -1 truthy है, तो एक missing @ pass हो जाएगा। सही test explicit है: if (email.indexOf("@") !== -1)। indexOf बिल्कुल एक number return करता है (B) और @ ढूँढ सकता है (C), तो 'हमेशा काम करता है' (D) ग़लत है।

Watch out

String traps

1. Result capture न करना: s.trim(), s.replace(...), s.toUpperCase() सब copies return करते हैं। उन्हें assign कीजिए।

2. indexOf को boolean की तरह test करना: यह एक position या -1 return करता है। हमेशा !== -1 compare कीजिए, truthiness पर कभी भरोसा मत कीजिए (0 एक valid position है पर falsy)।

3. slice बनाम substring: दोनों extract करते हैं, पर slice(-3) end से गिनता है जबकि substring negatives को 0 treat करता है। 'last N characters' चाहिए तो slice पकड़िए।

Theory

यह validation का दिल है

Unit 5 में आप एक email को यह check करके validate करेंगे कि इसमें एक @ और उसके बाद एक . है, और एक mobile number को spaces trim करके और length check करके, सब यहाँ वाले methods से। trim, indexOf, split, और slice form validation के रोज़ के tools हैं। अगला lesson इन reactions को user actions से events के साथ wire करता है।

Summary

Key takeaways

  • Strings immutable हैं: हर method एक NEW string return करता है, तो इसे capture कीजिए (name = name.trim())।
  • concat या + से join कीजिए; split(sep) एक string को एक array में तोड़ता है।
  • indexOf / lastIndexOf न मिलने पर एक position या -1 देते हैं: !== -1 से test कीजिए, कभी boolean की तरह नहीं।
  • slice, substring, charAt parts extract करते हैं; slice 'from the end' के लिए negative indices allow करता है।
  • trim() किनारे के spaces हटाता है; replace() पहला match swap करता है (सबके लिए replaceAll)।
  • Memory hook: string methods एक copy return करते हैं, तो इसे store कीजिए।

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Overview of JavaScript

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

JavaScript String and Events: string types; string functions (concat(), split(), indexOf(), lastIndexOf(), substring(), trim(), slice(), replace(), charAt()) · Web Designing-1 (option A) · Gri-Learn