String Literals คือชุดตัวอักษรใน Double Quotes ("") Wide-string Literals นำหน้าด้วย L เสมอ
ประเภท String Literals
| Prefix | ชื่อ | คำอธิบาย |
|---|---|---|
| (none) | Narrow String | Char ธรรมดา (char[]) |
| L | Wide String | Wide Char (wchar_t[]) |
| u8 | UTF-8 String | UTF-8 Encoded (char[]) |
| u | UTF-16 String | UTF-16 Encoded (char16_t[]) |
| U | UTF-32 String | UTF-32 Encoded (char32_t[]) |
| R | Raw String | Raw String Literal (C++11) |
ตัวอย่าง Wide String
#include <cwchar>
#include <cwctype>
#include <iostream>
using namespace std;
int main() {
wchar_t s[] = L"hello world!";
wcout << L"The uppercase string : \"is ";
for (int i = 0; i < wcslen(s); i++)
putwchar(towupper(s[i]));
return 0;
}
ผลลัพธ์
The uppercase string : "is HELLO WORLD!
Raw String Literal (C++11)
R"(This is a "raw" string with \n no escaping needed)"
ใช้สำหรับ Regex, File Path, HTML/XML ที่มีอักขระพิเศษมาก