/****************************************************************************** * This program demonstrates basic string functions. * * Copyright © 2020 Richard Lesh. All rights reserved. *****************************************************************************/ #include #include #include #include std::locale utf8loc(std::locale(), new std::codecvt_utf8); using namespace std; int main(int argc, char **argv) { setlocale(LC_ALL, "en_US.UTF-8"); wcout.imbue(utf8loc); wcin.imbue(utf8loc); wstring const alphabet = L"abcdefghijklmnopqrstuvwxyzabc"; wstring const greekAlphabet = L"αβγδεζηθικλμνξοπρσςτυφχψωαβγ"; wstring const emoji = L"😃😇🥰🤪🤑😴🤒🥵🥶🤯🥳😎😥😱😡🤬💀👽🤖😺🙈🙉🙊😃😇🥰"; wcout << L"Length: " << alphabet.length() << endl; wcout << L"charAt(17): " << alphabet[17] << endl; wcout << L"codePointAt(17): " << (int)(alphabet[17]) << endl; wcout << L"substr(23, 26): " << alphabet.substr(23, 3) << endl; wcout << L"prefix(6): " << alphabet.substr(0, 6) << endl; wcout << L"right_tail(6): " << alphabet.substr(6) << endl; wcout << L"suffix(6): " << alphabet.substr(alphabet.length() - 6) << endl; wcout << L"find(\'def\'): " << alphabet.find(L"def") << endl; wcout << L"find(\'def\') is not found: " << (alphabet.find(L"def") == string::npos) << endl; wcout << L"find(\'bug\'): " << alphabet.find(L"bug") << endl; wcout << L"find(\'bug\') is not found: " << (alphabet.find(L"bug") == string::npos) << endl; wcout << L"rfind(\'abc\'): " << alphabet.rfind(L"abc") << endl; wcout << L"rfind(\'abc\') is not found: " << (alphabet.rfind(L"abc") == string::npos) << endl; wcout << L"rfind(\'bug\'): " << alphabet.rfind(L"bug") << endl; wcout << L"rfind(\'bug\') is not found: " << (alphabet.rfind(L"bug") == string::npos) << endl; wcout << L"Length: " << greekAlphabet.length() << endl; wcout << L"charAt(17): " << greekAlphabet[17] << endl; wcout << L"codePointAt(17): " << (int)(greekAlphabet[17]) << endl; wcout << L"substr(23, 26): " << greekAlphabet.substr(23, 3) << endl; wcout << L"prefix(6): " << greekAlphabet.substr(0, 6) << endl; wcout << L"right_tail(6): " << greekAlphabet.substr(6) << endl; wcout << L"suffix(6): " << greekAlphabet.substr(greekAlphabet.length() - 6) << endl; wcout << L"find(\'δεζ\'): " << greekAlphabet.find(L"δεζ") << endl; wcout << L"find(\'δεζ\') is not found: " << (greekAlphabet.find(L"δεζ") == string::npos) << endl; wcout << L"find(\'bug\'): " << greekAlphabet.find(L"bug") << endl; wcout << L"find(\'bug\') is not found: " << (greekAlphabet.find(L"bug") == string::npos) << endl; wcout << L"rfind(\'αβγ\'): " << greekAlphabet.rfind(L"αβγ") << endl; wcout << L"rfind(\'αβγ\') is not found: " << (greekAlphabet.rfind(L"αβγ") == string::npos) << endl; wcout << L"rfind(\'bug\'): " << greekAlphabet.rfind(L"bug") << endl; wcout << L"rfind(\'bug\') is not found: " << (greekAlphabet.rfind(L"bug") == string::npos) << endl; wcout << L"Length: " << emoji.length() << endl; wcout << L"charAt(16): " << emoji[16] << endl; wcout << L"codePointAt(16): " << (int)(emoji[16]) << endl; wcout << L"substr(20, 24): " << emoji.substr(20, 4) << endl; wcout << L"prefix(6): " << emoji.substr(0, 6) << endl; wcout << L"right_tail(6): " << emoji.substr(6) << endl; wcout << L"suffix(6): " << emoji.substr(emoji.length() - 6) << endl; wcout << L"find(\'😱😡🤬\'): " << emoji.find(L"😱😡🤬") << endl; wcout << L"find(\'😱😡🤬\') is not found: " << (emoji.find(L"😱😡🤬") == string::npos) << endl; wcout << L"find(\'bug\'): " << emoji.find(L"bug") << endl; wcout << L"find(\'bug\') is not found: " << (emoji.find(L"bug") == string::npos) << endl; wcout << L"rfind(\'😃😇🥰\'): " << emoji.rfind(L"😃😇🥰") << endl; wcout << L"rfind(\'😃😇🥰\') is not found: " << (emoji.rfind(L"😃😇🥰") == string::npos) << endl; wcout << L"rfind(\'bug\'): " << emoji.rfind(L"bug") << endl; wcout << L"rfind(\'bug\') is not found: " << (emoji.rfind(L"bug") == string::npos) << endl; return 0; }