Thuta Learning
C++
BasicProgrammingအခြေခံ

Strings

C++သင်ခန်းစာ 835 မိနစ်

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • std::string value တွေကို ဖန်တီး၊ ဆက်၊ နှိုင်းယှဉ်နိုင်ပြီး string literal နဲ့ ဘာကွာလဲ ရှင်းပြနိုင်ဖို့
  • [] နဲ့ .at() နဲ့ character တွေကို ဖတ်နိုင်ပြီး ဘယ်ဟာက bounds ကို စစ်လဲ သိဖို့
  • find() နဲ့ ရှာ၊ substr() နဲ့ ဖြတ်ယူပြီး std::string::npos ကို ကိုင်တွယ်နိုင်ဖို့
  • မြန်မာစာလို UTF-8 text အတွက် .size() က စာလုံးမဟုတ်ဘဲ byte ကို ရေတွက်တဲ့ အကြောင်း ရှင်းပြနိုင်ဖို့
  • Dangling view မဖြစ်စေဘဲ read-only text အတွက် std::string_view ကို သုံးနိုင်ဖို့

ရိုးရိုးလေး ရှင်းပြမယ်

ရိုးရိုးလေး ရှင်းရရင်

std::string ဆိုတာ စာသားကို သိမ်းထားတဲ့ variable ပါ — နာမည်၊ message၊ product code စတာတွေပေါ့။ အရွယ်အစား ပုံသေ သေတ္တာနဲ့ မတူဘဲ character ထည့်တာ၊ ဖယ်တာ လုပ်တဲ့အခါ ကြီးလိုက် သေးလိုက် ဖြစ်ပြီး scope အပြင်ရောက်တဲ့အခါ သူ့ memory ကို သူကိုယ်တိုင် ရှင်းပါတယ်။ <string> ကို include လုပ်၊ std::string name = "Sai"; လို့ ရေးလိုက်ရင် အဲဒီကစပြီး + နဲ့ ဆက်တာ၊ size မေးတာ၊ character တစ်လုံးချင်း ဖတ်တာ၊ အထဲမှာ ရှာတာတွေ လုပ်လို့ ရပါပြီ။

"Sai" လို double quote ထဲက စာသားက သူ့ဘာသာ std::string မဟုတ်ပါဘူး။ C ကနေ အမွေဆက်ခံလာတဲ့ character array ပုံသေ ဖြစ်တဲ့ string literal ပါ။ အချိန်အများစုမှာ C++ က အလိုအလျောက် ပြောင်းပေးပေမယ့် ဒီကွာခြားချက်ကို သိထားရင် ဒီ lesson မှာ တွေ့ရမယ့် ရှုပ်ထွေးတဲ့ compile error တချို့ကို နားလည်သွားပါလိမ့်မယ်။

Problem → Why → Solution

ပြဿနာ: domain မဟုတ်တဲ့ domain

Sign-up form တစ်ခုက e-mail address တွေကို သိမ်းထားပြီး report တစ်ခုအတွက် address တစ်ခုချင်းရဲ့ domain အပိုင်း လိုပါတယ်။ Code က @ သင်္ကေတကို ရှာပြီး သူ့နောက်က အားလုံးကို ယူပါတယ်။ Test data ထဲက address တိုင်းအတွက် မှန်ပါတယ်။ အဲဒီနောက် user တစ်ယောက်က @ လုံးဝ မပါဘဲ aye.aye လို့ ရိုက်ထည့်လိုက်တော့ report က aye.aye ကို domain အဖြစ် ဂုဏ်ယူစွာ စာရင်းသွင်းပါတယ်။ အဲဒီ string တိုတိုလေးရဲ့ character နံပါတ် 20 ကို ဖတ်တဲ့ ဒုတိယ line ကတော့ စက်ပေါ်မူတည်ပြီး အမှိုက်တွေ ဒါမှမဟုတ် ဘာမှ မထုတ်ပါဘူး။

domain.cpp
#include <iostream>
#include <string>

int main() {
    std::string email = "aye.aye";               // the user forgot the @
    std::size_t at = email.find('@');            // not found
    std::string domain = email.substr(at + 1);   // what is at + 1?
    std::cout << "Domain: " << domain << '\n';   // Domain: aye.aye
    std::cout << email[20] << '\n';              // no bounds check
    return 0;
}

ဘာကြောင့် error မတက်ဘဲ မှားသွားတာလဲ

find() က မတွေ့တဲ့အခါ -1 ကို ပြန်မပေးပါဘူး။ std::size_t တစ်ခု သိမ်းနိုင်တဲ့ အကြီးဆုံး value ဖြစ်တဲ့ std::string::npos ကို ပြန်ပေးပါတယ်။ Unsigned arithmetic က လှည့်ပတ် (wrap) တတ်လို့ အဲဒါကို 1 ပေါင်းလိုက်ရင် 0 ပြန်ဖြစ်သွားပြီး substr(0) က string တစ်ခုလုံးကို တိတ်တဆိတ် ပြန်ပေးပါတယ်။ operator[] ကလည်း index ကို ဘယ်တော့မှ မစစ်ပါဘူး — character 7 လုံးပဲ ရှိတဲ့ string ရဲ့ position 20 ကို ဖတ်တာက error message မဟုတ်ဘဲ undefined behaviour ပါ။ find() ရဲ့ ရလဒ်ကို std::string::npos နဲ့ အမြဲ နှိုင်းယှဉ်ပါ၊ index က range ပြင်ပ ဖြစ်နိုင်ရင် .at() ကို သုံးပါ။

Core concept: string က byte တွေကို ပိုင်ဆိုင်တယ်

std::string က standard library ထဲက class တစ်ခုဖြစ်ပြီး အရွယ်အစား ပြောင်းလို့ရတဲ့ char buffer တစ်ခုကို ပိုင်ဆိုင်ပါတယ်။ char တစ်ခုက byte တစ်ခုပါ။ String က သူ့ထဲမှာ byte ဘယ်နှခု ရှိလဲ မှတ်ထားပြီး .size() (ဒါမှမဟုတ် အမွှာညီအစ်ကို .length()) က အဲဒါကို unsigned type ဖြစ်တဲ့ std::size_t အဖြစ် ပြန်ပေးပါတယ်။ String ကို = နဲ့ copy လုပ်တာ ဒါမှမဟုတ် by value ပို့တာက လုံးဝ သီးခြား copy တစ်ခု ဖန်တီးလို့ တစ်ခုကို ပြောင်းရင် နောက်တစ်ခုကို မထိခိုက်ပါဘူး။ String နှစ်ခုကို ==, != နဲ့ < နဲ့ content အလိုက် နှိုင်းယှဉ်ပြီး character code တွေကို ဘယ်ကနေ ညာကို တိုက်စစ်ပါတယ်။

"Sai" လို string literal ရဲ့ type က const char[4] ပါ — စာလုံး သုံးလုံးနဲ့ မမြင်ရတဲ့ '\0' terminator တစ်ခုပါ။ + operator က ဘက်တစ်ဖက်ဖက်မှာ std::string ရှိမှသာ စာသားကို ဆက်ပေးလို့ firstName + " " က အလုပ်လုပ်ပေမယ့် "Hello, " + "world" ကတော့ C++ က array နှစ်ခုကို ပေါင်းဖို့ ကြိုးစားတာမို့ compile error ဖြစ်ပါတယ်။ std::string တစ်ခုကနေ ဘယ်မှညာ စတည်ဆောက်တာ ဒါမှမဟုတ် std::string("Hello, ") + "world" လို့ ရေးတာနဲ့ ပြေလည်ပါတယ်။

Position တွေက 0 ကနေ စပါတယ်။ s[i] က ဘာမှ မစစ်ဘဲ character ကို ဖတ်လို့ အဆုံးကျော်တဲ့ index က undefined behaviour ပါ၊ s.at(i) ကတော့ စစ်ပြီး std::out_of_range ကို throw လုပ်ပါတယ်။ s.find(text) က ပထမဆုံး တွေ့တဲ့ position ကို ပြန်ပေးပြီး မတွေ့ရင် std::string::npos ကို ပေးပါတယ်။ s.substr(pos, count) က အပိုင်းတစ်ပိုင်းကို copy ကူးပေးပြီး count ရဲ့ default က "အဆုံးအထိ" ပါ။ C++20 က အဖြစ်များတဲ့ prefix နဲ့ suffix စစ်ဆေးမှုတွေအတွက် s.starts_with() နဲ့ s.ends_with() ကို ထပ်ထည့်ပေးထားပါတယ်။

နောက်ဆုံးအနေနဲ့ size က စာလုံးကို မဟုတ်ဘဲ byte ကို ရေတွက်ပါတယ်။ ဒီနေ့ source file နဲ့ terminal တွေက UTF-8 ကို အမြဲလိုလို သုံးပြီး အဲဒီမှာ English စာလုံးတစ်လုံးက byte တစ်ခု ယူပေမယ့် မြန်မာ code point တစ်ခုစီက byte သုံးခု ယူပါတယ်။ std::string("မြန်မာ").size() က 18 ပါ — ဖတ်တဲ့သူက ဝဏ္ဏ နှစ်လုံးပဲ မြင်ရပေမယ့် code point ခြောက်ခု မြှောက် byte သုံးခုပါ။ ဒီလို string ကို [] နဲ့ index လုပ်ရင် character တစ်ခုရဲ့ အပိုင်းအစ သက်သက်ဖြစ်တဲ့ byte တစ်ခုတည်းကိုပဲ ရပါတယ်။

Expression (s = "Sai Tun")ရလဒ်မှတ်ချက်
s.size()7byte အရေအတွက်၊ std::size_t
s[0]'S'bounds မစစ်
s.at(10)throwstd::out_of_range
s.find(' ')3ပထမဆုံး တွေ့တဲ့ position
s.find('@')std::string::nposမတွေ့
s.substr(4)"Tun"index 4 ကနေ အဆုံးအထိ copy
String တစ်ခုနဲ့ အလုပ်လုပ်ပုံ
  1. String က 0 ကနေ နံပါတ်တပ်ထားတဲ့ byte တွေကို ပိုင်တယ်
  2. ဘက်တစ်ဖက်က string ဖြစ်မှ + က ဆက်ပေးတယ်
  3. find က position ဒါမှမဟုတ် npos ပေးတယ်
  4. substr က တောင်းတဲ့ အပိုင်းကို copy ကူးတယ်

String က ပေးတဲ့ number တိုင်း (size, position, npos) က unsigned std::size_t ဖြစ်လို့ အဲဒါနဲ့ arithmetic မလုပ်ခင် အရင်စစ်ပါ။

အထွေထွေ ပုံစံ

cpp
#include <string>

std::string text = "literal";          // owns a copy of the characters
std::string joined = text + " more";   // one side must be a std::string
std::size_t n = text.size();           // bytes, as an unsigned size_t
char first = text[0];                  // unchecked; text.at(0) checks
std::size_t pos = text.find("lit");    // std::string::npos if missing
std::string piece = text.substr(pos, count);

std::string ကို literal ကနေ ဖန်တီးပြီး + နဲ့ ဆက်ပါတယ်၊ size() က byte အရေအတွက်ကို ပေးပါတယ်။ [] က မစစ်ဘဲ ဖတ်ပြီး at() က စစ်ပါတယ်။ find() ရဲ့ ရလဒ်ကို substr() ဆီ မပို့ခင် std::string::npos နဲ့ အရင် နှိုင်းယှဉ်ပါ။

Example: နာမည်တစ်ခုကို ဆက်၊ တိုင်း၊ ရှာ

ဒီ program က နာမည်ပိုင်း နှစ်ခုကို ဆက်ပြီး ရလာတဲ့ string ကို စစ်ဆေးပါတယ် — size၊ ပထမ character၊ space ရှိတဲ့ နေရာ၊ family name၊ မပါတဲ့ @ ကို ရှာတာ စတာတွေပါ။ နောက်ဆုံး line နှစ်ကြောင်းက မြန်မာ မြို့နာမည်တစ်ခုကို တိုင်းပြီး byte နဲ့ စာလုံး မတူတာကို ပြပါတယ်။

strings.cpp
#include <iostream>
#include <string>

int main() {
    std::string firstName = "Sai";
    std::string lastName = "Tun";
    std::string fullName = firstName + " " + lastName;

    std::cout << "Full name: " << fullName << '\n';
    std::cout << "Length: " << fullName.size() << '\n';
    std::cout << "First letter: " << fullName[0] << '\n';

    std::size_t space = fullName.find(' ');
    std::cout << "Space at index: " << space << '\n';
    std::cout << "Family name: " << fullName.substr(space + 1) << '\n';
    std::cout << std::boolalpha
              << "Has @? " << (fullName.find('@') != std::string::npos) << '\n';

    fullName += " (student)";
    std::cout << "Updated: " << fullName << '\n';

    std::string city = "မန္တလေး";
    std::cout << "Bytes in city name: " << city.size() << '\n';
    return 0;
}

Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်

  1. Joiningstd::string fullName = firstName + " " + lastName;

    Expression ကို ဘယ်ကနေ ညာကို တွက်ပြီး firstName က std::string ဖြစ်ပြီးသားမို့ + အလုပ်လုပ်ပါတယ် — firstName + " " က std::string အသစ်တစ်ခု ထုတ်ပေးပြီး အဲဒါကို lastName နဲ့ ဆက်ပါတယ်။

  2. Size in bytesfullName.size()

    Byte ခုနစ်ခုပါ — စာလုံး သုံးလုံး၊ space တစ်ခု၊ စာလုံး သုံးလုံး။ ရိုးရိုး English text မှာ byte နဲ့ စာလုံး ကိုက်ညီလို့ 7 က မြင်ရတဲ့ character အရေအတွက်လည်း ဖြစ်ပါတယ်။

  3. Index 0fullName[0]

    Position တွေ 0 ကနေ စလို့ [0] က S ပါ။ [] က bounds မစစ်ပါဘူး — fullName.at(0) ကလည်း S ကိုပဲ ပေးပေမယ့် index 7 နဲ့ အထက်ဆိုရင် std::out_of_range ကို throw လုပ်ပါလိမ့်မယ်။

  4. Find and slicefullName.substr(space + 1)

    find(' ') က space ရဲ့ index ဖြစ်တဲ့ 3 ကို ပြန်ပေးလို့ substr(4) က index 4 ကနေ အဆုံးအထိ copy ကူးပြီး Tun ရပါတယ်။ Space ရှိကြောင်း သေချာသိထားလို့ပဲ ဒီမှာ လုံခြုံတာပါ၊ နောက် line က အရင်မေးနည်းကို ပြပါတယ်။

  5. Not found(fullName.find('@') != std::string::npos)

    @ မရှိလို့ find က std::string::npos ကို ပြန်ပေးပြီး comparison က false ဖြစ်ပါတယ်။ "တွေ့သလား" ဆိုတာကို စစ်တဲ့ နည်းမှန်က npos နဲ့ နှိုင်းယှဉ်တာပါ။

  6. AppendingfullName += " (student)";

    += က ရှိပြီးသား string ရဲ့ နောက်မှာ နေရာမှာတင် ထပ်ဆက်ပါတယ်၊ လိုအပ်သလို string က သူ့ buffer ကို ချဲ့ပါတယ်။

  7. UTF-8 bytescity.size()

    မန္တလေး မှာ code point ခုနစ်ခု ရှိပြီး တစ်ခုစီကို UTF-8 byte သုံးခုနဲ့ သိမ်းလို့ size() က 21 လို့ ပြောပါတယ်၊ ဖတ်တဲ့သူ ရေတွက်မယ့် စာလုံးအရေအတွက် မဟုတ်ပါဘူး။

You should see
Full name: Sai Tun
Length: 7
First letter: S
Space at index: 3
Family name: Tun
Has @? false
Updated: Sai Tun (student)
Bytes in city name: 21

find နဲ့ npos ကို သုံးပြီး လုံခြုံစွာ ရှာခြင်း

User ဒါမှမဟုတ် file ကနေ text ဝင်လာတိုင်း ကိုယ်ရှာနေတဲ့ အရာက မရှိနိုင်ဘူးလို့ ယူဆပါ။ find() ရဲ့ ရလဒ်ကို သိမ်းထား၊ std::string::npos နဲ့ နှိုင်းယှဉ်ပြီးမှ ဖြတ်ယူပါ။ အောက်မှာ သုံးထားတဲ့ if statement ကို နောက် lesson မှာ အသေးစိတ် လေ့လာမှာပါ၊ ဒီမှာတော့ ဘာမှ မတွေ့ရင် ဖြတ်ယူတာကို ကျော်ဖို့ပဲ သုံးထားပါတယ်။

email.cpp
#include <iostream>
#include <string>

int main() {
    std::string email = "thida@example.com";
    std::size_t at = email.find('@');
    if (at != std::string::npos) {
        std::cout << "User: " << email.substr(0, at) << '\n';
        std::cout << "Domain: " << email.substr(at + 1) << '\n';
    }

    std::string typo = "aye.aye";
    if (typo.find('@') == std::string::npos) {
        std::cout << "No @ in " << typo << '\n';
    }
    return 0;
}

std::string_view: copy မဟုတ်ဘဲ ပြတင်းပေါက်တစ်ခု

std::string ကို by value ပို့ရင် သူ့ byte အားလုံးကို copy ကူးပါတယ်။ Text ကို ဖတ်ရုံပဲ လိုတဲ့ နေရာတွေအတွက် C++17 ရဲ့ std::string_view က ပိုသက်သာပါတယ် — သူက တခြားသူ ပိုင်တဲ့ character တွေဆီ ညွှန်တဲ့ pointer နဲ့ length တစ်ခုပဲ ဖြစ်ပြီး std::string object တွေရော string literal တွေကိုပါ လက်ခံပါတယ်။ သူ့ member function တွေက std::string ရဲ့ read-only ဘက်ခြမ်းနဲ့ တူပါတယ်: size(), find(), substr(), starts_with() နဲ့ ends_with()။ string_view ပေါ်က substr() က copy မကူးဘဲ view အသစ်တစ်ခုကိုပဲ ပြန်ပေးပါတယ်။

view.cpp
#include <iostream>
#include <string>
#include <string_view>

int main() {
    std::string title = "Learn C++ step by step";
    std::string_view view = title;               // no copy: points into title
    std::string_view firstWord = view.substr(0, 5);

    std::cout << firstWord << '\n';
    std::cout << view.size() << " bytes viewed\n";
    std::cout << std::boolalpha << view.starts_with("Learn") << '\n';
    return 0;
}

View က သူ့ text ထက် ပိုအသက်ရှည်လို့ မရပါ

std::string_view က character တွေကို ဘယ်တော့မှ မပိုင်လို့ သူညွှန်ထားတဲ့ string ထက် ပိုကြာကြာ အသက်ရှင်လို့ မရပါဘူး။ std::string_view bad = std::string("temp") + "orary"; ကို g++ -Wall -Wextra က warning တစ်ခုမှ မပြဘဲ compile လုပ်ပေးပါတယ်၊ ဒါပေမယ့် temporary string က အဲဒီ line အဆုံးမှာ ပျက်စီးသွားပြီး bad က free လုပ်ပြီးသား memory ကို ညွှန်နေပါတယ်။ အဲဒါကို ဖတ်တာက undefined behaviour ပါ။ View တွေကို parameter နဲ့ local အတိုအတောင်း သုံးဖို့ပဲ ထားပြီး text ကို ဆက်ထိန်းထားရမယ်ဆိုရင် std::string ကို သိမ်းပါ။

လက်တွေ့ project တွေမှာ

Real project တွေမှာ ဘယ်လို သုံးလဲ

Program တိုင်းလိုလိုက သူ့အစွန်းတွေမှာ text ကို ကိုင်တွယ်ရပါတယ်။ Web server တွေက /users/42 လို request path ကို slash နဲ့ ခွဲပါတယ်၊ command-line tool တွေက starts_with("--") ဖြစ်တဲ့ flag တွေကို ရှာပါတယ်၊ log analyser တွေက line တိုင်းမှာ keyword တစ်ခုကို find() လုပ်ပြီး configuration loader တွေက key=value line တွေကို = နေရာမှာ နှစ်ပိုင်း ဖြတ်ပါတယ်။ ဒါတွေ အားလုံးက ဒီ lesson ထဲက ပုံစံအတိုင်း find() နဲ့ substr() ကို npos စစ်ဆေးမှု သေချာနဲ့ တွဲသုံးတာပါပဲ။

Byte နဲ့ စာလုံး ကွာခြားချက်က ဒီ site လို ဘာသာစကား နှစ်မျိုးသုံး site တွေမှာ ကိုက်ပါတယ်။ Character 50 လို့ ကန့်သတ်ထားတဲ့ database column၊ character ရေတွက်တဲ့ SMS gateway ဒါမှမဟုတ် title ကို substr(0, 20) နဲ့ ဖြတ်တဲ့ UI တစ်ခုက မြန်မာ စကားလုံးကို byte သုံးခု sequence ရဲ့ အလယ်မှာ ဖြတ်ပြီး ကျိုးနေတဲ့ သင်္ကေတတွေ ပြနိုင်ပါတယ်။ Real project တွေက text ကို ICU လို Unicode ကို နားလည်တဲ့ library နဲ့ တိုင်းပြီး ဖြတ်တာ ဒါမှမဟုတ် text ကို အပြည့်ထားပြီး truncation ကို UI layer ကို လွှဲပေးတာ လုပ်ပါတယ်။

အမှားများ

အဖြစ်များတဲ့ အမှားတွေ

1. String literal နှစ်ခုကို ပေါင်းတာ

"Hello, " + "world" က error: invalid operands of types 'const char [8]' and 'const char [6]' to binary 'operator+' နဲ့ fail ဖြစ်ပါတယ်။ ဘယ်ဘက်ကမှ std::string မဟုတ်လို့ ခေါ်စရာ + မရှိပါဘူး။ std::string ကနေ စပါ (std::string("Hello, ") + "world") ဒါမှမဟုတ် literal တစ်ခုတည်း "Hello, world" လို့ပဲ ရေးပါ။

2. char ကို literal နဲ့ ပေါင်းတာ

email[0] + "***" က ဆက်တာလို ထင်ရပေမယ့် char နဲ့ const char* ပေါင်းတာက pointer arithmetic ပါ — char ရဲ့ code က pointer ကို ရှေ့တိုးလိုက်ပြီး ရလဒ်က literal ရဲ့ အပြင်ဘက် တစ်နေရာကို ညွှန်နေပါတယ်။ char ကို အရင် wrap လုပ်ပါ: std::string(1, email[0]) + "***"။

3. find() ရဲ့ ရလဒ်ကို bool အဖြစ် စစ်တာ

if (s.find("x")) က match က position 0 မဟုတ်တဲ့ ဘယ်နေရာမှာ ရှိရှိ true ဖြစ်ပါတယ် — npos က သုည မဟုတ်လို့ ဘာမှ မတွေ့တဲ့အခါမှာလည်း true ပါပဲ။ String ရဲ့ အစမှာ တွေ့တဲ့ match ကတော့ false ဖြစ်သွားပါတယ်။ တစ်ခုတည်းသော မှန်ကန်တဲ့ စစ်ဆေးမှုက s.find("x") != std::string::npos ပါ။

4. size() ကို စာလုံးအရေအတွက်လို့ ယူဆတာ

မြန်မာစာ ဒါမှမဟုတ် emoji text အတွက် size() က UTF-8 byte အရေအတွက်ပါ။ Memory နဲ့ protocol ကန့်သတ်ချက်တွေအတွက် သုံးပါ၊ ဒါပေမယ့် user တွေကို character အရေအတွက် ကတိပေးဖို့ ဒါမှမဟုတ် substr() နဲ့ text ကို ဖြတ်ဖို့ မသုံးပါနဲ့။

Best Practices

အကောင်းဆုံး အလေ့အကျင့်များ

  • <iostream> က ဆွဲယူလာမယ်လို့ အားမကိုးဘဲ <string> ကို ကိုယ်တိုင် include လုပ်ပါ။
  • find() ရဲ့ ရလဒ်ကို std::size_t ထဲ သိမ်းပြီး မဖြတ်ယူခင် std::string::npos နဲ့ နှိုင်းယှဉ်ပါ။
  • လေ့လာနေတုန်း ဒါမှမဟုတ် index က input ကနေ လာတဲ့အခါ .at() ကို သုံးပါ၊ bounds ကို သက်သေပြပြီးသား နေရာတွေမှာပဲ [] ကို ပြောင်းသုံးပါ။
  • Read-only text parameter တွေကို std::string_view ဒါမှမဟုတ် const std::string& နဲ့ ယူပြီး caller က ပိုင်ရမယ့် ရလဒ်ကို std::string အဖြစ် return ပြန်ပါ။

Mini challenge

Mini challenge: file name စစ်ဆေးသူ

Document manager တစ်ခုက upload လုပ်ထားတဲ့ file တွေအကြောင်း အချက်အလက် ပြဖို့ လိုပါတယ်။ ဒီ lesson ထဲက string tool တွေနဲ့ file name တစ်ခုကို အပိုင်းပိုင်း ခွဲတဲ့ program တစ်ခု ရေးပါ။

  • file ထဲက နောက်ဆုံး dot ကို rfind('.') နဲ့ ရှာပြီး base name နဲ့ extension ကို substr နဲ့ print ထုတ်ပါ
  • file က ".pdf" နဲ့ ဆုံးသလားကို ends_with (C++20) နဲ့ true/false ထုတ်ပါ
  • Extension ကို "bak" နဲ့ အစားထိုးထားတဲ့ backup name တစ်ခု တည်ဆောက်ပါ
  • "README" မှာ dot ပါသလားကို rfind ရဲ့ ရလဒ်ကို std::string::npos နဲ့ နှိုင်းယှဉ်ပြီး ထုတ်ပါ
file_name.cpp
#include <iostream>
#include <string>

int main() {
    std::string file = "report.final.pdf";
    std::string noExtension = "README";

    // TODO: find the LAST dot in file (look up rfind)
    // TODO: print the base name and the extension with substr
    // TODO: print whether file ends with ".pdf" (C++20 ends_with)
    // TODO: build and print a backup name that ends in ".bak"
    // TODO: print whether noExtension contains a dot (compare with npos)
    return 0;
}

ရလဒ်: Output က ဒီလို ထွက်ရပါမယ်: Base name: report.final Extension: pdf Is PDF? true Backup name: report.final.bak README has a dot? false

အရိပ်အမြွက် ကြည့်ရန်

rfind() က find() လိုပဲ အလုပ်လုပ်ပေမယ့် အဆုံးကနေ ရှာလို့ report နောက်က dot ကို မဟုတ်ဘဲ pdf ရှေ့က dot ကို တွေ့ပါတယ်။ substr(0, dot) က dot ရှေ့က အားလုံးကို copy ကူးပြီး substr(dot + 1) က dot နောက်က အားလုံးကို copy ကူးပါတယ်။

အဖြေ ကြည့်ရန်
file_name.cpp
#include <iostream>
#include <string>

int main() {
    std::string file = "report.final.pdf";
    std::string noExtension = "README";

    std::size_t dot = file.rfind('.');
    std::string base = file.substr(0, dot);
    std::string extension = file.substr(dot + 1);
    std::cout << "Base name: " << base << '\n';
    std::cout << "Extension: " << extension << '\n';

    std::cout << std::boolalpha;
    std::cout << "Is PDF? " << file.ends_with(".pdf") << '\n';

    std::string backup = base + ".bak";
    std::cout << "Backup name: " << backup << '\n';

    bool hasDot = noExtension.rfind('.') != std::string::npos;
    std::cout << noExtension << " has a dot? " << hasDot << '\n';
    return 0;
}

Quick Quiz 1 / 3

UTF-8 system တစ်ခုပေါ်မှာ std::string("မြန်မာ").size() က ဘာကို ပြန်ပေးမလဲ။

Quick Quiz 2 / 3

ဘယ် line က compile မဖြစ်ဘူးလဲ။

Quick Quiz 3 / 3

std::string s = "report"; ဆိုရင် s.find('z') က ဘာလဲ။

မှတ်ထားရမယ့် အချက်များ

  • std::string က ကြီးထွားနိုင်တဲ့ byte buffer ကို ပိုင်ပါတယ်၊ copy တွေက သီးခြားဖြစ်ပြီး == က content ကို နှိုင်းယှဉ်ပါတယ်။
  • String literal တွေက const char array တွေဖြစ်လို့ + အတွက် အနည်းဆုံး operand တစ်ခုက std::string ဖြစ်ရပါမယ်။
  • [] က မစစ်ပါဘူး၊ .at() က std::out_of_range ကို throw လုပ်ပါတယ်၊ ဘာမှ မတွေ့ရင် find() က std::string::npos ကို ပြန်ပေးပါတယ်။
  • size() က UTF-8 byte တွေကို ရေတွက်လို့ မြန်မာ text အတွက် code point အရေအတွက်ရဲ့ သုံးဆလောက် ပြပါတယ်။
  • std::string_view က သက်သာတဲ့ read-only view ဖြစ်ပြီး သူညွှန်တဲ့ text ထက် ဘယ်တော့မှ ပိုအသက်ရှည်လို့ မရပါဘူး။

The Current ISO C++ Standard (C++23) — Standard C++ Foundation

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။

Strings | Thuta Learning