Theory
Librarian एक Human की तरह Type करती है
इस week front desk से real input:
" let us c " (stray spaces), "" (Nothing पर Enter pressed), "Programming in ANSI C by Balagurusamy" (सिर्फ़ title part चाहिए)।
BookBridge को इन सबको clean, test, slice और search करना है। Java की String class exactly इसके लिए एक toolbox ship करती है, और आपका syllabus 11 tools नाम लेता है। इन्हें 2 jobs वाली एक family की तरह सीखिए: methods जो एक string को inspect करते हैं, और methods जो एक नई produce करते हैं (कभी न भूलिए: immutable, हमेशा नई)।
Theory
पहले, Java की तरह Count करना सीखिए
नीचे का हर method indexes में बोलता है, 0 से शुरू होते हुए। यह example pin कीजिए:
"Let Us C"
L e t ␣ U s ␣ C
0 1 2 3 4 5 6 7
तो length() 8 है (count), पर last index 7 है (count minus 1)। charAt(4) 'U' है। Count और last index को mix up करना StringIndexOutOfBoundsException को cause करता है जिससे आपके आधे batch की इस week मुलाक़ात होगी।
At a glance
Inspectors: पूछते हैं, कभी बदलते नहीं
| Method | Answer करता है | "Let Us C" पर |
|---|---|---|
| length() | कितने characters? | 8 |
| charAt(4) | Index 4 पर कौन सा char बैठता है? | 'U' |
| indexOf("s") | इस text का पहला index, या -1 | 5 |
| lastIndexOf(" ") | इस text का आख़िरी index, या -1 | 6 |
| isEmpty() | क्या length() == 0? | false |
| equals(s) | Same characters? (पिछला lesson) | content check |
At a glance
Producers: हर result एक NEW string है
| Method | Produce करता है | Example |
|---|---|---|
| trim() | बिना leading/trailing spaces वाली copy | " let us c ".trim() → "let us c" |
| substring(4) | Index 4 से आख़िर तक | "Let Us C" → "Us C" |
| substring(0, 3) | Indexes 0,1,2: end EXCLUSIVE | "Let Us C" → "Let" |
| concat(s) | यह string + s जुड़ी हुई | "Book".concat("Bridge") → "BookBridge" |
| split(" ") | Pieces की String[] | "Let Us C" → {"Let","Us","C"} |
| String.join("-", parts) | एक separator से glued pieces (static) | join("-","a","b") → "a-b" |
Practical
एक Messy Entry को End to End Clean करना
public class CleanTitle {
public static void main(String[] args) {
String raw = " let us c ";
String t = raw.trim(); // "let us c"
System.out.println(t.length()); // 8
System.out.println(t.isEmpty()); // false
System.out.println(t.indexOf("us")); // 4
System.out.println(t.charAt(0)); // l
String[] words = t.split(" "); // {"let","us","c"}
System.out.println(words.length); // 3 (field, no brackets!)
System.out.println(String.join("-", words)); // let-us-c
}
}
Quiz
"Let Us C".substring(4, 6) क्या return करता है?
- "Us"
- "Us "
- " Us"
- "s C"
Show the answer
"Us"
substring(begin, end) indexes begin से end EXCLUSIVE तक लेता है: indexes 4 और 5, characters 'U' और 's', तो "Us"। Option B index 6 (space) include करता है, उन students के लिए classic off-by-one जो end को inclusive पढ़ते हैं। एक quick self-check जो examiners खुद इस्तेमाल करते हैं: result की length हमेशा end minus begin है, यहाँ 6 - 4 = 2 characters। अगर आपके answer की length ग़लत है, indexes फिर से पढ़िए।
Think first
-1 Sentinel
t.indexOf("Python") जब t "let us c" है: इसमें कहीं भी Python नहीं है। Tap करने से पहले: क्या वापस आता है, और वही particular value क्यों?
Show the answer
-1। यह 0 या कोई भी positive number नहीं हो सकता, क्योंकि उनमें से हर एक एक real index है जहाँ एक match हो SAKTA है; -1 वह एक integer है जो कभी एक position नहीं हो सकता, तो यह safely "not found" का मतलब है। BookBridge में हर जगह working idiom: if (t.indexOf(q) != -1) { found }। Check भूलना और -1 को substring या charAt में एक index की तरह इस्तेमाल करना एक instant runtime exception है।
Watch out
तीन Habits जो Marks खोती हैं
length() बनाम length: एक String पर यह brackets वाला एक method है, एक array पर बिना brackets वाला एक field: t.length() पर words.length। Exams एक snippet में दोनों plant करते हैं, exactly हमारी listing जैसे।
Results नई strings हैं यह भूलना: t.trim(); अकेले कुछ clean नहीं करता; assign कीजिए: t = t.trim();
charAt(t.length()) एक 8-character string के index 8 के लिए पूछता है: StringIndexOutOfBoundsException। आख़िरी character length() - 1 पर रहता है।
Theory
इन्हें Plumbing की तरह Compose कीजिए
Methods chain होते हैं, क्योंकि हर producer अगले call के लिए ready एक String return करता है: raw.trim().substring(0, 3).equals("let") clean, slice, test की तरह पढ़ा जाता है। वह composability यही वजह है ये 11 ज़्यादातर text work cover करते हैं जो BookBridge को कभी चाहिए होगा। जब job एक बढ़ते text की heavy repeated EDITING बन जाए, new-string producers को chain करना wasteful हो जाता है, और यही exactly अगले lesson के StringBuffer का cue है।
Summary
Key takeaways
- Indexes 0 से start होते हैं; length() characters count करता है, last index length() - 1 है।
- Inspectors: length, charAt, indexOf, lastIndexOf, isEmpty, equals।
- Producers (हमेशा एक NEW string): trim, substring, concat, split, और static String.join।
- substring(begin, end) end-exclusive है; result length = end - begin।
- indexOf/lastIndexOf not found के लिए -1 return करते हैं: इस्तेमाल करने से पहले check कीजिए।
- String length() में brackets हैं; array length में नहीं।
- Memory hook: inspectors पूछते हैं, producers आपको एक fresh copy देते हैं।