Learn String in C++ using simple and easy to understand snippets.

Snippet 1: String Unicode

String unicode example.

CMakeLists.txt

cmake_minimum_required(VERSION 3.1)

project(StringUnicode)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

if (MSVC)
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /utf-8")
endif ()

add_executable(StringUnicode StringUnicode.cpp)

StringUnicode.cpp

#include <iostream>
#include <string>

#if WIN32
#include <Windows.h>
#else
int SetConsoleOutputCP(int codePage) {return 1;}
#endif

using namespace std;
using namespace string_literals;

auto main(int argc, char* argv[]) -> int {
  auto arabic = "\u0627\u0644\u0643\u0644\u0645\u0629 \u0627\u0644\u0639\u0631\u0628\u064A\u0629"s;
  auto tradChinese = "\u4E2D\u570B\u7684\u6F22\u5B57"s;
  auto simplChinese = "\u6C49\u8BED"s;
  auto french = "Langue fran\u00E7aise"s;
  auto greek = "\u0395\u03BB\u03BB\u03B7\u03BD\u03B9\u03BA\u03AE \u03B3\u03BB\u03CE\u03C3\u03C3\u03B1"s;
  auto hebrew = "\u05DB\u05EA\u05D1 \u05E2\u05D1\u05E8\u05D9\u05EA"s;
  auto hindi = "\u0939\u093f\u0928\u094d\u0926\u0940 \u092d\u093e\u0937\u093e"s;
  auto icelandic = "\u00CDslenska"s;
  auto japanese = "\u65E5\u672C\u8A9E\u306E\u3072\u3089\u304C\u306A, \u6F22\u5B57\u3068\u30AB\u30BF\u30AB\u30CA"s;
  auto korean = "\uB300\uD55C\uBBFC\uAD6D\uC758 \uD55C\uAE00"s;
  auto russian = "\u0420\u0443\u0441\u0441\u043A\u0438\u0439 \u044F\u0437\u044B\u043A"s;

  SetConsoleOutputCP(65001);
  cout << "Arabic:         " << arabic << endl;
  cout << "Trad. Chinese:  " << tradChinese << endl;
  cout << "Simpl. Chinese: " << simplChinese << endl;
  cout << "French:         " << french << endl;
  cout << "Greek:          " << greek << endl;
  cout << "Hebrew:         " << hebrew << endl;
  cout << "Hindi:          " << hindi << endl;
  cout << "Icelandic:      " << icelandic << endl;
  cout << "Japanese:       " << japanese << endl;
  cout << "Korean:         " << korean << endl;
  cout << "Russian:        " << russian << endl;
}

This code produces the following output:

 Arabic:         الكلمة العربية
 Trad. Chinese:  中國的漢字
 Simpl. Chinese: 汉语
 French:         Langue française
 Greek:          Ελληνική γλώσσα
 Hebrew:         כתב עברית
 Hindi:          हिन्दी भाषा
 Icelandic:      Íslenska
 Japanese:       日本語のひらがな, 漢字とカタカナ
 Korean:         대한민국의 한글
 Russian:        Русский язык

Snippet 2: String Unicode - Second Example

A second String Unicode example.

CMakeLists.txt

cmake_minimum_required(VERSION 3.1)

project(StringUnicode2)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

if (MSVC)
  add_definitions(-D_CRT_SECURE_NO_WARNINGS)
endif ()

add_executable(StringUnicode2 StringUnicode2.cpp)

StrigUnicode2.cpp

#include <iomanip>
#include <iostream>
#include <string>
#include <sstream>

#if WIN32
#include <Windows.h>
#else
int SetConsoleOutputCP(int codePage) {return 1;}
#endif

using namespace std;
using namespace string_literals;

auto main() -> int {
  auto str = L"\u65E5\u672C\u8A9E\u306E\u3072\u3089\u304C\u306A\u002c\u0020\u6F22\u5B57\u3068\u30AB\u30BF\u30AB\u30CA\U0001F428\u0021"s;

  SetConsoleOutputCP(65001);

  auto result = L""s;
  for(wchar_t c : str) {
    wstringstream ss;
    ss << c << L" ";
    result += ss.str();
  }
  auto s = string(result.size() * sizeof(wchar_t), 0);
  wcstombs(&s[0], result.c_str(), s.size() * sizeof(wchar_t));
  cout << s << endl;

  result = L"";
  for(wchar_t c : str) {
    wstringstream ss;
    ss << (c > 0xFFFF ? L"\\U" : L"\\u") << right << setfill(L'0') << setw(c > 0xFFFF ? 8 : 4) << hex << (int)c;
    result += ss.str();
  }
  s = string(result.size() * sizeof(wchar_t), 0);
  wcstombs(&s[0], result.c_str(), s.size() * sizeof(wchar_t));
  cout << s << endl;
}

Here is what you get:

// 日 本 語 の ひ ら が な ,   漢 字 と カ タ カ ナ 🐨 !
// \u65e5\u672c\u8a9e\u306e\u3072\u3089\u304c\u306a\u002c\u0020\u6f22\u5b57\u3068\u30ab\u30bf\u30ab\u30ca\U0001f428\u0021

Snippet 3: String Conversion

String Conversion example.

CMakeLists.txt

cmake_minimum_required(VERSION 3.1)
project(StringConversion)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

if (MSVC)
  add_definitions(-D_CRT_SECURE_NO_WARNINGS)
endif ()

add_executable(StringConversion StringConversion.cpp)

StringConversion.cpp

#include <iostream>
#include <codecvt>
#include <locale>
#include <numeric>

using namespace std;

auto main() -> int {
  auto s = "こんにちは世界!"s;

  wstring ws(s.size(), 0);
  mbstowcs(&ws[0], s.c_str(), s.size());
}

Snippet 4: String Split Example

How to split a string in C++.

CMakeLists.txt

cmake_minimum_required(VERSION 3.1)

project(StringSplit)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_executable(StringSplit StringSplit.cpp)

StringSplit.cpp

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using namespace std;
using namespace std::literals;

vector<string> split_string(const string& str, const vector<char>& splitCharSeparators = {}, uint32_t count = std::numeric_limits<uint32_t>::max(), bool removeEmptyEntries = false) {
  if (count == 0) return {};
  if (count == 1) return {str};

  vector<string> list;
  string subString;
  vector<char> separators = splitCharSeparators.size() == 0 ? vector<char> {9, 10, 11, 12, 13, 32} : splitCharSeparators;
  for (string::const_iterator it = str.begin(); it != str.end(); it++) {
    bool isSeparator =  std::find(separators.begin(), separators.end(), *it) != separators.end();
    if (!isSeparator) subString.append(string(1, *it));
    if ((it - str.begin() == str.length() - 1 || isSeparator) && (subString.length() > 0 || (subString.length() == 0 && removeEmptyEntries == false))) {
      if (list.size() == count - 1) {
        list.push_back(subString + string(str.c_str(), it - str.begin() + (isSeparator ? 1 : 0), str.length() - (it - str.begin()) + (isSeparator ? 1 : 0)));
        return list;
      }
      list.push_back(subString);
      subString.clear();
    }
  }

  return list;
}

auto main() -> int {
  auto strings = split_string("One Two Three Four\nFive");

  cout << "strings::Length = " << strings.size() << endl;
  for (auto s : strings)
    cout << s << endl;
}

This code produces the following output:

 strings.Length = 5
 One
 Two
 Three
 Four
 Five

Snippet 5: String split with Regex

How to split a string with regex.

CMakeLists.txt

cmake_minimum_required(VERSION 3.1)

project(StringSplitWithRegex)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_executable(StringSplitWithRegex StringSplitWithRegex.cpp)

StringSplitWithRegex.cpp

#include <iostream>
#include <string>
#include <regex>
#include <vector>

using namespace std;
using namespace std::literals;

auto main() -> int {
  auto str = "One Two Three Four\nFive"s;
  regex rgx("(\x9|\xA|\xB|\xC|\xD|\x20)+");

  vector<string> strings;
  for (std::sregex_token_iterator it(str.begin(), str.end(), rgx, -1), end; it != end; ++it)
    strings.push_back(*it);

  cout << "strings::Length = " << strings.size() << endl;
  for (auto s : strings)
    std::cout << s << '\n';
}

This code produces the following output:

 strings.Length = 5
 One
 Two
 Three
 Four
 Five