Introduction

Strings are objects that represent sequences of characters. The standard string class provides a simple, safe and versatile alternative to using explicit arrays of chars when dealing with text and other sequences of characters. The C++ string class is part of the std namespace and was standardized in 1998.

Syntax

std::string s;

std::string s(“Hello”);

std::string s = “Hello”;

std::string s1(“Hello”);

std::string s2(s1);

std::string s1(“Hello”);

std::string s2(s1, 0, 4); // Copy 4 characters from position 0 of s1 into s2

std::string s1(“Hello World”);

std::string s2(s1, 5); // Copy first 5 characters of s1 into s2

std::string s(5, ‘a’); // s contains aaaaa