String က စာသားများကို သိမ်းဖို့သုံးပါတယ်။ User name, title, message, address, product name လို text data တွေကို string နဲ့ ကိုင်တွယ်နိုင်ပါတယ်။ C++ မှာ string သုံးချင်ရင် <string> header ကို include လုပ်ထားတာက ပိုသေချာပါတယ်။
cpp
#include <iostream>
#include <string>
using namespace std;
int main() {
string firstName = "Sai";
string lastName = "Tun";
string fullName = firstName + " " + lastName;
cout << "Full name: " << fullName << endl;
cout << "Length: " << fullName.length() << endl;
cout << "First letter: " << fullName[0];
return 0;
}+ ကို string တွေကို ဆက်ဖို့ သုံးနိုင်ပါတယ်။ length() က string ထဲမှာ character ဘယ်နှစ်လုံးရှိလဲ ပြန်ပေးပါတယ်။ fullName[0] က ပထမဆုံး character ကို ရယူတာပါ။ Index က 0 ကနေ စတာကို သတိထားပါ။
You should see
Full name: Sai Tun Length: 7 First letter: SInfo
String index က 0-based ဖြစ်ပါတယ်။ ပထမစာလုံးက index 0, ဒုတိယစာလုံးက index 1 ပါ။