Mục tiêu:
- Hiểu cách xử lý, cắt, ghép, tìm kiếm, thay thế chuỗi.
- Sử dụng các hàm
substr(),find(),erase(),replace(), v.v. - Làm quen với chuỗi kiểu
stringstream– dùng để phân tách dữ liệu nhanh. - Ứng dụng: Xử lý dữ liệu văn bản thực tế (ví dụ chat log, input người dùng)
1. Ôn lại kiểu string
Mã:
#include <iostream>
#include <string>
using namespace std;
int main() {
string s = "Hello World!";
cout << s << " co " << s.size() << " ky tu." << endl;
}
Mã:
Hello World! co 12 ky tu.
2. Cắt chuỗi – substr(start, length)
Mã:
#include <iostream>
#include <string>
using namespace std;
int main() {
string s = "Harvard University";
string word = s.substr(8, 10);
cout << word << endl;
}
Mã:
University
s.substr(8, 10)nghĩa là: lấy từ ký tự thứ 8, 10 ký tự.
3. Tìm kiếm chuỗi – find()
Mã:
#include <iostream>
#include <string>
using namespace std;
int main() {
string s = "C++ is powerful and elegant.";
size_t pos = s.find("powerful");
if (pos != string::npos)
cout << "'powerful' xuat hien tai vi tri " << pos << endl;
}
Mã:
'powerful' xuat hien tai vi tri 7
4. Xóa chuỗi con – erase(pos, length)
Mã:
#include <iostream>
#include <string>
using namespace std;
int main() {
string s = "I hate bugs!";
s.erase(2, 5); // Xóa từ vị trí 2, 5 ký tự
cout << s << endl;
}
Mã:
I bugs!
5. Thay thế chuỗi con – replace(pos, len, new_string)
Mã:
#include <iostream>
#include <string>
using namespace std;
int main() {
string s = "I hate bugs!";
s.replace(2, 4, "love coding");
cout << s << endl;
}
Mã:
I love coding bugs!
6. Biến đổi chữ hoa / chữ thường
Mã:
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main() {
string s = "Harvard";
transform(s.begin(), s.end(), s.begin(), ::toupper);
cout << s << endl;
transform(s.begin(), s.end(), s.begin(), ::tolower);
cout << s << endl;
}
Mã:
HARVARD
harvard
7. Cắt chuỗi thành từng phần – stringstream
Mã:
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main() {
string data = "apple,banana,orange";
stringstream ss(data);
string token;
while (getline(ss, token, ',')) {
cout << token << endl;
}
}
Mã:
apple
banana
orange
8. Đếm số từ trong chuỗi
Mã:
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main() {
string sentence = "I love learning C++ at Harvard!";
stringstream ss(sentence);
string word;
int count = 0;
while (ss >> word) count++;
cout << "Co " << count << " tu trong cau." << endl;
}
Mã:
Co 6 tu trong cau.
9. Bài tập tổng hợp: Kiểm tra từ khóa trong đoạn văn
Mã:
#include <iostream>
#include <string>
using namespace std;
int main() {
string text = "Harvard is one of the most prestigious universities in the world.";
string keyword = "prestigious";
if (text.find(keyword) != string::npos)
cout << "Tu '" << keyword << "' ton tai trong doan van!" << endl;
else
cout << "Khong tim thay!" << endl;
}
Mã:
Tu 'prestigious' ton tai trong doan van!
Minh họa trực quan:
Mã:
┌──────────────────────────────────────┐
│ "Harvard is one of the top schools." │
└──────────────────────────────────────┘
↓ Tìm từ khóa
┌─────────────┐
│ "Harvard" │
│ "top" │
│ "schools" │
└─────────────┘
TỔNG KẾT TIẾT 16
| Hàm | Chức năng | Ví dụ |
|---|---|---|
substr(a,b) | Cắt chuỗi | "Hello".substr(1,3) → "ell" |
find(x) | Tìm chuỗi con | s.find("abc") |
erase(a,b) | Xóa ký tự | Xóa từ vị trí a, b ký tự |
replace(a,b,c) | Thay chuỗi | Thay từ a dài b ký tự bằng c |
stringstream | Cắt dữ liệu | Dùng khi có dấu phẩy hoặc khoảng trắng |
