A std::string is an type in C++ used to represent a sequence of char types.
You can use the std::string class by including the string system include file with the statement
// include C++ std::string #include <string>Note that
// include old C-style strings #include <string.h>is completely different.
You can use the full name std::string, or say
using namespace std;This allows the shorter string name to work as well.
// include C++ std::string
#include <string>
// include basic stream input and output features
#include <iostream>
// allow string instead of std::string
using namespace std;
int main()
{
string a; // a is initally empty
string b="bob";
// report current values of a and b
cout << "a=\"" << a << "\"" << endl;
cout << "b=\"" << b << "\"" << endl;
a = b; // a is a copy of b now.
b = "don";
// report current values of a and b
cout << "a=\"" << a << "\"" << endl;
cout << "b=\"" << b << "\"" << endl;
// the length() method gives back a
// string's length:
cout << "a is " << a.length() << " long" << endl;
cout << "b is " << b.length() << " long" << endl;
// the + operator concatenates strings
a = b + "ny";
cout << "a=\"" << a << "\"" << endl;
// the append() method appends to the string
a.append(" and marie"); // same as: a = a + " and marie";
cout << "a=\"" << a << "\"" << endl;
// the index operator [] gives access to the individual
// chars that make up the string.
cout << "The first char of a is " << a[0] << endl;
cout << "The last char of a is " << a[a.length()-1] << endl;
// The relational operators are defined for string objects.
a = "bob";
b = "Bob";
if (a == b) {
cout << "a == b is true" << endl;
} else {
cout << "a == b is false" << endl;
}
return 0;
}