String Methods (String နဲ့ သုံးတဲ့ method များ)
B2.1.2 - Construct programs that can extract and manipulate substrings.
မြန်မာလို: substring (string ထဲက အပိုင်း တစ်ခု) တွေကို ထုတ်ယူနိုင်ပြီး ပြင်ဆင် ကိုင်တွယ်နိုင်တဲ့ program တွေကို ကိုယ်တိုင် တည်ဆောက်နိုင်ရပါမယ်။
အဓိက အယူအဆများ
String (စာသား) က primitive type (Java ရဲ့ အခြေခံ ဒေတာ အမျိုးအစား) မဟုတ်ပါဘူး၊ built-in method (Java မှာ အသင့်ပါတဲ့ method) တွေ ပါတဲ့ object (class တစ်ခုကနေ ဖန်တီးထားတဲ့ instance) တစ်ခုပါ။ String တွေက immutable (ဖန်တီးပြီးရင် ပြောင်းလို့ မရတဲ့) ဖြစ်ပါတယ်: ဘယ် String method (သီးခြား အလုပ်တစ်ခု လုပ်ပေးတဲ့ code အပိုင်း) ကမှ မူလ String ကို မပြောင်းပါဘူး။ toUpperCase()၊ substring() လို method တွေက ရလဒ်ကို String တစ်ခုအဖြစ် return (ရလဒ်အဖြစ် ပြန်ပေး) လုပ်ပါတယ်၊ အဲဒီ ရလဒ်ကို လိုရင် ကိုယ်တိုင် သိမ်းထားရပါတယ်။
အမြန် ကိုးကားရန် ဇယား
| Method | ပြန်ပေးတဲ့ တန်ဖိုး | ဥပမာ |
|---|---|---|
.length() | Character (စာလုံး ဒါမှမဟုတ် သင်္ကေတ) အရေအတွက် | "Hi".length() → 2 |
.charAt(i) | Index (နေရာအမှတ်) i မှာ ရှိတဲ့ char | "Hi".charAt(1) → 'i' |
.substring(s) | Index s ကနေ အဆုံးအထိ | "Hello".substring(2) → "llo" |
.substring(s, e) | s ကနေ e အထိ (e ကို မထည့်) | "Hello".substring(1, 4) → "ell" |
.indexOf(str) | str စတဲ့ index၊ မတွေ့ရင် -1 | "Hello".indexOf("ll") → 2 |
.toUpperCase() | စာလုံးကြီး အားလုံး | "hi".toUpperCase() → "HI" |
.toLowerCase() | စာလုံးသေး အားလုံး | "HI".toLowerCase() → "hi" |
.equals(str) | အကြောင်းအရာ တူရင် true | "a".equals("a") → true |
.equalsIgnoreCase(str) | စာလုံး အကြီး အသေး မခွဲဘဲ တူရင် true | "A".equalsIgnoreCase("a") → true |
.compareTo(str) | Negative (အနုတ်ကိန်း) / 0 / positive (အပေါင်းကိန်း) | "a".compareTo("b") → negative |
(Python မှာ ဒါတွေနဲ့ ညီမျှတာတွေက len(s)၊ s[i]၊ s[a:b] slicing (string ရဲ့ အပိုင်းကို ဖြတ်ယူခြင်း)၊ s.find(...)၊ s.upper()၊ s.lower() နဲ့ နှိုင်းယှဉ်ဖို့ ရိုးရိုး == / < / > တို့ ဖြစ်ပါတယ်။)
အဖြစ်များတဲ့ ထောင်ချောက်:
substring(start, end)ကstartကနေendအထိ character တွေကို ပြန်ပေးပါတယ်၊ ဒါပေမယ့်endကိုတော့ မထည့်ပါဘူး။"Hello".substring(1, 4)က"ell"ကို ပေးပါတယ် (index 1, 2, 3)၊ index 4 ကို ချန်ထားပါတယ်။
String တွေကို အမြဲ
.equals()နဲ့ နှိုင်းယှဉ်ပါ၊==နဲ့ ဘယ်တော့မှ မနှိုင်းယှဉ်ပါနဲ့။==operator က အကြောင်းအရာကို မဟုတ်ဘဲ memory reference (object ရှိတဲ့ memory နေရာကို ညွှန်းထားတာ) တွေကို နှိုင်းယှဉ်ပါတယ်။ (Python မှာတော့ ပြောင်းပြန်ပါ:==က အကြောင်းအရာကို နှိုင်းယှဉ်ပြီး မှန်ကန်တဲ့ နည်းပါ၊.equals()မရှိပါဘူး။)
ဖြေရှင်းပြထားတဲ့ ဥပမာများ
ဥပမာ 1: အခြေခံ String method များ
String name = "Hello, World!";
System.out.println(name.length()); // 13
System.out.println(name.charAt(0)); // 'H'
System.out.println(name.charAt(7)); // 'W'
System.out.println(name.substring(7)); // "World!"
System.out.println(name.substring(7, 12)); // "World"
System.out.println(name.indexOf("World")); // 7
System.out.println(name.indexOf("Java")); // -1
System.out.println(name.toUpperCase()); // "HELLO, WORLD!"
System.out.println(name.toLowerCase()); // "hello, world!"
name = "Hello, World!"
# Python uses len(), indexing s[i], and slicing s[start:end] instead of methods
print(len(name)) # 13
print(name[0]) # "H"
print(name[7]) # "W"
print(name[7:]) # "World!"
print(name[7:12]) # "World" - the end index is still excluded
print(name.find("World")) # 7
print(name.find("Java")) # -1
print(name.upper()) # "HELLO, WORLD!"
print(name.lower()) # "hello, world!"
အဓိက အချက်များ:
- Index တွေက 1 ကနေ မဟုတ်ဘဲ 0 ကနေ စပါတယ်
.length()မှာ ကွင်း()ပါပါတယ် (method call ဖြစ်လို့ပါ)။ ကွင်း မပါတဲ့ array (အရွယ်အစား ပုံသေ၊ အမျိုးအစားတူ ဒေတာစု) ရဲ့.lengthနဲ့ နှိုင်းယှဉ်ကြည့်ပါ- Substring ကို ရှာမတွေ့ရင်
.indexOf()က -1 ကို ပြန်ပေးပါတယ်
ဥပမာ 2: နာမည် အပြည့်အစုံကို parsing (စာသားကို အပိုင်းတွေ ခွဲထုတ်ခြင်း) လုပ်ခြင်း
String fullName = "Alice Johnson";
int spaceIndex = fullName.indexOf(" "); // 5
String firstName = fullName.substring(0, spaceIndex); // "Alice"
String lastName = fullName.substring(spaceIndex + 1); // "Johnson"
System.out.println("First: " + firstName);
System.out.println("Last: " + lastName);
System.out.println("Total characters: " + fullName.length()); // 13
full_name = "Alice Johnson"
space_index = full_name.find(" ") # 5
first_name = full_name[0:space_index] # "Alice"
last_name = full_name[space_index + 1:] # "Johnson" - open-ended slice = "to the end"
print("First: " + first_name)
print("Last: " + last_name)
print("Total characters: " + str(len(full_name))) # 13
ပုံစံ: indexOf() နဲ့ delimiter (အပိုင်းတွေကို ခြားပေးတဲ့ character) ကို ရှာပြီး substring() နဲ့ ခွဲပါ။
ဥပမာ 3: String တွေကို နှိုင်းယှဉ်ခြင်း
String s1 = "apple";
String s2 = "apple";
String s3 = "APPLE";
System.out.println(s1.equals(s2)); // true
System.out.println(s1.equals(s3)); // false (case matters)
System.out.println(s1.equalsIgnoreCase(s3)); // true
System.out.println("apple".compareTo("banana")); // negative (a < b)
System.out.println("cat".compareTo("cat")); // 0 (equal)
System.out.println("dog".compareTo("cat")); // positive (d > c)
s1 = "apple"
s2 = "apple"
s3 = "APPLE"
# Python compares string CONTENT with == - there is no .equals()
print(s1 == s2) # True
print(s1 == s3) # False (case matters)
print(s1.lower() == s3.lower()) # True - the equalsIgnoreCase pattern
# There is no compareTo() either - use < and > for alphabetical order
print("apple" < "banana") # True ("apple" comes before)
print("cat" == "cat") # True (equal)
print("dog" > "cat") # True ("dog" comes after)
compareTo() က ပြန်ပေးတာ:
- Negative → ပထမ string က အက္ခရာစဉ်အရ အရင် လာပါတယ်
- Zero (သုည) → string နှစ်ခု တူပါတယ်
- Positive → ပထမ string က အက္ခရာစဉ်အရ နောက်မှ လာပါတယ်
ဥပမာ 4: Character တစ်လုံးချင်း ဆက်ပြီး String တည်ဆောက်ခြင်း
String word = "Hello";
String reversed = "";
for (int i = word.length() - 1; i >= 0; i--) {
reversed = reversed + word.charAt(i);
}
System.out.println(reversed); // "olleH"
word = "Hello"
reversed_word = "" # "reversed" is a built-in function name in Python, so we rename
for i in range(len(word) - 1, -1, -1): # counts len-1 down to 0
reversed_word = reversed_word + word[i]
print(reversed_word) # "olleH"
String ကို character တစ်လုံးချင်း ဖြတ်သွားတာ (Java မှာ
charAt(i)၊ Python မှာs[i]) က စာသားထဲမှာ ရေတွက်တာ၊ ရှာတာ၊ ပြောင်းပြန်လှန်တာတွေရဲ့ နောက်ကွယ်က ပုံစံပါ။
ဥပမာ 5: Character တွေကို ရေတွက်ခြင်း
String sentence = "The quick brown fox";
int spaceCount = 0;
for (int i = 0; i < sentence.length(); i++) {
if (sentence.charAt(i) == ' ') {
spaceCount++;
}
}
System.out.println("Spaces: " + spaceCount); // 3
sentence = "The quick brown fox"
space_count = 0
for i in range(len(sentence)):
if sentence[i] == " ": # no char type in Python - " " is a one-character string
space_count = space_count + 1
print("Spaces:", space_count) # 3
Single quote နဲ့ ရေးထားတဲ့
== ' 'ကို သတိပြုပါ။ ဒီမှာcharတန်ဖိုး (primitive) တွေကို နှိုင်းယှဉ်နေတာ ဖြစ်လို့==က မှန်ပါတယ်။.equals()ကို String object တွေအတွက်ပဲ သုံးပါ။ (Python မှာchartype သီးခြား မရှိပါဘူး၊==က string အားလုံးကို အရှည် ဘယ်လောက်ပဲ ရှိရှိ နှိုင်းယှဉ်ပါတယ်။)
Quick Check (အမြန် စစ်ဆေးမေးခွန်း)
Q1. "Hello".substring(1, 4) က ဘာကို ပြန်ပေးသလဲ။
Q2. "Hello".indexOf("lo") က ဘာကို ပြန်ပေးသလဲ။
Q3. "Hello".indexOf("xyz") က ဘာကို ပြန်ပေးသလဲ။
Q4. ဒီ code ကို run ပြီးတဲ့အခါ word ရဲ့ တန်ဖိုးက ဘာလဲ။String word = "hello"; word.toUpperCase();
Q5. String တစ်ခုရဲ့ အရှည်ကို ယူဖို့ ဘယ်ဟာက မှန်သလဲ။
Trace Exercise (code ကို တစ်ဆင့်ချင်း လိုက်ကြည့်ခြင်း)
ဒီ code ကို trace လုပ်ပါ။ Variable တစ်ခုချင်းစီရဲ့ တန်ဖိုးကို ဖြည့်ပါ။
String word = "Computer";
int len = word.length();
char first = word.charAt(0);
String sub = word.substring(3, 7);
String upper = word.toUpperCase();
word = "Computer"
length = len(word) # "len" is the built-in function's name, so the variable is renamed
first = word[0]
sub = word[3:7]
upper = word.upper()
အကြံပြုချက်: C=0, o=1, m=2, p=3, u=4, t=5, e=6, r=7
| Line | Variable | တန်ဖိုး |
|---|---|---|
word.length() | len | |
word.charAt(0) | first | |
word.substring(3, 7) | sub | |
word.toUpperCase() | upper |
Trace Exercise 2: Parsing
ဒီ code ကို trace လုပ်ပါ။ တန်ဖိုးတွေကို အဆင့်ဆင့် တွက်ကြည့်ပါ။
String email = "alice@school.edu";
int atIndex = email.indexOf("@");
String username = email.substring(0, atIndex);
String domain = email.substring(atIndex + 1);
int dotIndex = domain.indexOf(".");
String extension = domain.substring(dotIndex + 1);
email = "alice@school.edu"
at_index = email.find("@")
username = email[0:at_index]
domain = email[at_index + 1:]
dot_index = domain.find(".")
extension = domain[dot_index + 1:]
| Variable | တန်ဖိုး |
|---|---|
atIndex | |
username | |
domain | |
dotIndex | |
extension |
Code Completion (code ကွက်လပ် ဖြည့်ခြင်း)
ဒီ လေ့ကျင့်ခန်းက string ကို delimiter နေရာမှာ ခွဲတဲ့ အသုံးများတဲ့ ပုံစံကို လေ့ကျင့်ပေးပါတယ်: indexOf() နဲ့ space ကို ရှာပြီး substring() နဲ့ space ရဲ့ ရှေ့က အပိုင်းနဲ့ နောက်က အပိုင်းကို ထုတ်ယူပါ။ နာမည် အပြည့်အစုံကနေ first name (ပထမ နာမည်) နဲ့ last name (နောက်ဆုံး နာမည်၊ မျိုးရိုး နာမည်) ကို ထုတ်ယူတဲ့ code ကို ဖြည့်ပါ။
String fullName = "John Smith";
int space = fullName.;
String first = fullName.;
String last = fullName.substring();
System.out.println("First: " + first); // John
System.out.println("Last: " + last); // Smith Spot the Error (အမှား ရှာခြင်း)
ဒီ code က if statement ကို သုံးပြီး string နှစ်ခုရဲ့ အကြောင်းအရာ တူမတူ စစ်ရမှာပါ၊ ဒါပေမယ့် comparison operator (နှိုင်းယှဉ်တဲ့ operator) ကို မှားသုံးထားပါတယ်။ (ဒီ code လို file တစ်ခုတည်းထဲမှာ literal (code ထဲမှာ တိုက်ရိုက် ရေးထားတဲ့ တန်ဖိုး) နှစ်ခု ရှိရင် Java က တူညီတဲ့ literal တွေကို ပြန်သုံးလို့ Match! လို့ တိုက်ဆိုင်ပြီး print ထွက်ပါတယ်။ User ရိုက်ထည့်တဲ့ စကားလုံး ဆိုရင်တော့ အဲဒီ စကားလုံးက hello ဖြစ်နေရင်တောင် ဘာမှ print မထွက်ပါဘူး။ ဘယ်လိုပဲ ဖြစ်ဖြစ် == က မှားတဲ့ စစ်နည်းပါ။) Bug (code ထဲက အမှား) ကို ရှာပါ။
String input = "hello";
String expected = "hello";
if (input == expected) {
System.out.println("Match!");
}ဘာမှားနေသလဲ၊ ဘယ်လို ပြင်မလဲ။
ဒီ code က charAt() ကို သုံးပြီး စကားလုံး တစ်လုံးရဲ့ နောက်ဆုံး character ကို print ထုတ်ရမှာပါ၊ ဒါပေမယ့် StringIndexOutOfBoundsException (index က string ရဲ့ အပိုင်းအခြားကို ကျော်သွားလို့ program run နေစဉ် ဖြစ်တဲ့ အမှား) နဲ့ program ရပ်သွားပါတယ်။ ဒီ bug က zero-based indexing (index ကို 0 ကနေ စရေတွက်ခြင်း) ကို နားလည်မလည် စစ်ပါတယ်။ Bug ကို ရှာပါ။
String word = "Java";
char last = word.charAt(word.length());
System.out.println("Last char: " + last);ဘာမှားနေသလဲ၊ ဘယ်လို ပြင်မလဲ။
Predict the Output (output ကို ကြိုခန့်မှန်းခြင်း)
ဒီ code က အဓိက String method သုံးခု ဖြစ်တဲ့ substring()၊ charAt() နဲ့ indexOf() ကို string တစ်ခုတည်းပေါ်မှာ သုံးပြပါတယ်။ Index တွေက 0 ကနေ စတယ်ဆိုတာနဲ့ substring(start, end) က end index ကို မထည့်ဘူးဆိုတာ သတိရပြီး method call တစ်ခုချင်းကို သေသေချာချာ trace လုပ်ပါ။
ဒီ code က ဘာကို print ထုတ်မလဲ။
String s = "Programming";
System.out.println(s.substring(0, 7));
System.out.println(s.charAt(3));
System.out.println(s.indexOf("gram"));
s = "Programming"
print(s[0:7])
print(s[3])
print(s.find("gram"))
Program
g
3ဒီ code က မူလ string ရဲ့ character တွေကို charAt() နဲ့ နောက်ပြန် အစဉ်အတိုင်း တစ်လုံးချင်း ဖြတ်သွားပြီး string အသစ် တစ်ခုကို တည်ဆောက်ပါတယ်။ ဒါက loop (ထပ်ခါထပ်ခါ run တဲ့ code အပိုင်း) နဲ့ ဖြတ်သွားခြင်းကို string concatenation (string တွေကို ဆက်ခြင်း) နဲ့ ပေါင်းသုံးတဲ့၊ လူသိများတဲ့ string ပြောင်းပြန်လှန်တဲ့ algorithm (အယ်လ်ဂိုရီသမ်၊ ပြဿနာကို အဆင့်ဆင့် ဖြေရှင်းတဲ့ နည်းလမ်း) ပါ။
ဒီ code က ဘာကို print ထုတ်မလဲ။
String word = "Hello";
String result = "";
for (int i = word.length() - 1; i >= 0; i--) {
result = result + word.charAt(i);
}
System.out.println(result);
word = "Hello"
result = ""
for i in range(len(word) - 1, -1, -1):
result = result + word[i]
print(result)
Practice Exercises (လေ့ကျင့်ခန်းများ)
Core (အခြေခံ အဆင့်)
-
Domain ထုတ်ယူခြင်း။
"www.google.com"လို URL (website လိပ်စာ) တစ်ခုကို ဖတ်ပြီး"google"ကိုပဲ ထုတ်ယူပါ။ -
ပထမ character ကို အစားထိုးခြင်း။ စကားလုံး တစ်လုံးကို ဖတ်ပြီး ပထမ စာလုံးကို
'X'နဲ့ အစားထိုးပါ (string ကို ပြန်တည်ဆောက်ဖို့substring()ကို သုံးပါ)။ -
File extension ထုတ်ယူခြင်း။ User ဆီကနေ file နာမည် တစ်ခုကို ဖတ်ပါ (ဥပမာ
"MyClass.java")။ File extension (file နာမည်ထဲက အစက်နောက်မှာ ရှိတဲ့ file အမျိုးအစား ပြတဲ့ အပိုင်း) ကို ထုတ်ယူပြီး output ထုတ်ပါ။ နည်းလမ်း: အစက်ကို ရှာဖို့indexOf(".")၊ extension ကို ထုတ်ယူဖို့substring(dotIndex + 1)ကို သုံးပါ။ စဉ်းစားကြည့်ပါ: အစက် မပါရင် ဘာဖြစ်သင့်သလဲ။
Extension (တိုးချဲ့ အဆင့်)
-
စကားလုံး ရေတွက်ခြင်း။ စာကြောင်း တစ်ကြောင်းထဲက စကားလုံးတွေကို ရေတွက်ပါ (
count spaces + 1: space အရေအတွက်ကို 1 ပေါင်းပါ)။ -
Palindrome စစ်ခြင်း။
charAt()ကို သုံးပြီး စကားလုံး တစ်လုံးက ရှေ့ကနေ ဖတ်ဖတ်၊ နောက်ကနေ ဖတ်ဖတ် အတူတူ ဖြစ်လား (palindrome ဖြစ်လား) စစ်ပါ။ -
Initials method။
public static String getInitials(String fullName)ဆိုတဲ့ method ကို ရေးပါ။ အဲဒီ method က နာမည်တွေရဲ့ ပထမ စာလုံးတွေ (initials) ကို ပြန်ပေးရပါမယ်၊ ဥပမာ"Alice Johnson"အတွက်"A.J."ပါ။
Challenge (စိန်ခေါ်မှု အဆင့်)
-
Caesar cipher (စာလုံးတွေကို ပုံသေ နေရာ အရေအတွက် ရွှေ့ပြီး လျှို့ဝှက်တဲ့ နည်း)။
charAt()နဲ့ type casting (data type တစ်ခုကနေ တစ်ခုကို ပြောင်းခြင်း) ကို သုံးပြီး character တစ်လုံးချင်းကို N နေရာစီ ရွှေ့ပါ။ -
Title case (စကားလုံးတိုင်းရဲ့ ပထမ စာလုံးကို စာလုံးကြီး ပြောင်းခြင်း)။
"hello world from java"ကို"Hello World From Java"အဖြစ် ပြောင်းပါ။
Exam-Style (စာမေးပွဲ ပုံစံ မေးခွန်း၊ 15 marks)
[3 marks] စတာတွေက အပိုင်း တစ်ခုချင်းစီအတွက် ရနိုင်တဲ့ အမှတ်ပါ။ IB စာမေးပွဲ မေးခွန်းတွေမှာ အမှတ်ကို ဒီလို ပြလေ့ ရှိပါတယ်။
ကျောင်းသား record (အချက်အလက် အများကြီး စုထားတဲ့ ဒေတာ တစ်ခု) ခွဲထုတ်တဲ့ program။ "LastName, FirstName (ID)" ပုံစံ ရှိတဲ့ string တွေကို ပေးထားပါတယ်:
- (a) Last name ကို ထုတ်ယူပါ [3 marks]
- (b) First name ကို ထုတ်ယူပါ [3 marks]
- (c) ID ကို ကွင်း မပါဘဲ ထုတ်ယူပါ [3 marks]
- (d) ဒီလို record တွေပါတဲ့
String[]ကို လက်ခံပြီး ပေးထားတဲ့ last name ရှိတဲ့ record အရေအတွက်ကို ပြန်ပေးတဲ့ method တစ်ခု ရေးပါ [6 marks]
GitHub Classroom (အလိုအလျောက် စစ်ဆေးတဲ့ test တွေပါတဲ့ programming assignment များ)
A class assignment on GitHub Classroom: use it when your teacher has set it. It needs a free GitHub account.
Working in the Codespace
- Accept and open. Accept the assignment above, open the link in the email from GitHub, then press Open in Codespace. In Java, change the status bar from Lightweight to Standard mode.
- Write and check. Open
src/StringBasics.javaand complete eachTODO. Press Run and compare the output with the comments, or runbash run-tests.shin the Terminal. - Submit. In Source Control, write a message, press Commit, then Sync Changes. Your teacher’s feedback arrives as a pull request called Feedback.
There are four exercise files in src/: start with StringBasics.java and work down the list in the README.
ဆက်စပ် ခေါင်းစဉ်များ
အရင် သိထားရမှာများ:
- Variables & Data Types (variable နဲ့ data type များ): String ကို type တစ်ခုအဖြစ် နားလည်ခြင်း
ဆက်စပ် ခေါင်းစဉ်များ:
- Iteration (ထပ်ခါထပ်ခါ လုပ်ခြင်း): String ကို character တစ်လုံးချင်း ဖြတ်သွားဖို့ သုံးတဲ့ loop တွေ
- 1D Arrays (တန်းတစ်တန်းတည်းမှာ စီထားတဲ့ ဒေတာအစု): index နဲ့ ယူသုံးတဲ့ ပုံစံ ဆင်တူပါတယ်
နောက်ထပ် လေ့လာမယ့် ခေါင်းစဉ်များ:
- Exception Handling (program run နေစဉ် ဖြစ်တဲ့ အမှားတွေကို ကိုင်တွယ်ခြင်း): String operation (String ပေါ်မှာ လုပ်တဲ့ လုပ်ဆောင်ချက်) တွေ မအောင်မြင်တဲ့အခါ ဘာဖြစ်သလဲ
- Selection (condition ပေါ်မူတည်ပြီး လမ်းကြောင်း ရွေးခြင်း): condition တွေထဲမှာ String နှိုင်းယှဉ်ချက်တွေကို သုံးခြင်း