In C + +, the string class was introduced to facilitate the processing of strings. String types support variable-length strings.
Before using string, the corresponding header file must be included, string belongs to the STD named domain, and therefore need to be named qualified:
#include <string>
Using Std::string; using namespace Std;
definition and initialization of a string object
Examples of use are:
#include <iostream> #include <string>using std::cout;using std::endl;using std::string;int Main (int argc, Char *argv[]) {string S1 = "Mike"; cout << "S1 =" << s1 << Endl; S1 = mikestring S2 (s1); Initialize S2 to a copy of S1 cout << s2 = << s2 << endl; S2 = mikestring S3 ("Jiang"); Initialize S3 to "Mike" cout << "s3 =" << S3 << Endl; S3 = jiangstring S4 (' a '); The value of S4 is 10 ' a ' cout << ' s4 = ' << S4 << Endl; S1 = Mikereturn 0;}
The results of the operation are as follows:
string Object Common operations
The sample code is as follows:
#include <iostream> #include <string>using std::cout;using std::endl;using std::string;int Main (int argc, Char *argv[]) {string S1 = "Mike";//returns True if S1 is an empty string, otherwise returns Falseif (true = = S1.empty ()) {cout << "S1 is empty\n";} Else{cout << "S1 is no empty\n";} The size of the string, S1.size () is equivalent to S1.length () cout << "len =" << s1.size () << endl;//return S1 maximum capacity cout << "max_s ize = "<< s1.max_size () << endl;//remove each character for (int i = 0; I < s1.size (); i++) {//s1[i] equivalent to s1.at (i) cout << i << "=" << s1[i] << Endl;} String s2 = "Jiang"; cout << "S1 + s2 =" << s1+s2 << endl;//C + + string and c char * mutual conversion//String to con St Char *const char *str = s1.c_str () cout << "c_str =" << str << endl;const char *data = S2.data (); cou T << "data =" << data << endl;//char * to stringconst char *buf = "Hello, C + +";//string my_str = Strin g (BUF); string my_str = Buf;cout << "my_str =" << my_str << endl;//s1 and s2 string Exchange S1 = "Mike"; s2 = "Jiang"; s1.swap (S2); cout << "S1 =" << S1 << ", s2 =" <& Lt S2 << endl;s1.clear () S1 = "Mike"; cout << "S1 =" << S1 << endl;s1 = "Mikejiang";//Remove S1 from 2 onwards 4 characters, position starting from 0 s2 = s1.substr (2, 4); cout << "s2 =" << s2 << endl;s1 = "Mikejiang";//delete S1 2 characters after 4, bit Set starting from 0 s2 = s1.erase (2, 4); cout << "s2 =" << s2 << endl;s1 = "Mike";//s1 starting from 4 (position starting from 0), inserting "jiang" string s2 = S1.insert (4, "Jiang"); cout << "s2 =" << s2 << endl;s1= "Mikejiang"//delete S1 5 characters starting from 4, then insert "Tyson" at 5 places S2 = S1.replace (4, 5, "Tyson"), cout << "s2 =" << s2 << endl;s1= "Mikejiang";//Find the position of the string "J" in S1 (position starting from 0) int n = S1.find ("J"); cout << "n =" << n << endl;return 0;}
The results of the operation are as follows:
iterator action String Object
The string class provides a forward and backward traversal of the iterator iterator, which provides the syntax for accessing individual characters, similar to a pointer operation, where the iterator does not check the scope. The iterator variable is declared with String::iterator or string::const_iterator, and Const_iterator does not allow the content of the iteration to be changed.
The sample code is as follows:
#include <iostream> #include <string>using std::cout;using std::endl;using std::string;int Main (int argc, Char *argv[]) {string s= "Mike";//Initialize to "Mike"//Use an iterator to print out the values of all character elements String::const_iterator t;for (T=s.begin (); T!=s.end () ; t++) {cout<< *t << ",";} cout << Endl;string::iterator it;//assigns all character elements to a ' a ' for (It=s.begin (); It!=s.end (); it++) {*it = ' a ') via an iterator;} cout << endl;//The values of all elements are printed out by Iterators for (It=s.begin (); It!=s.end (); it++) {cout<< *it << ",";} cout << endl;s = "Mikejiang"; it = S.begin (); Returns a pointer to the element at the beginning of the string (iterator)//deletes the pointer it+1 the element that points to the position, and returns a pointer (iterator) s.erase (it+1) to the position of the next element; Remove "I" cout << endl << "after erase:\n";//The value of all elements is printed by an iterator for (It=s.begin (); It!=s.end (); it++) {cout& lt;< *it << ",";} cout << endl;s = "Mmmmmm"; it = S.begin (); After location it inserts 3 ' a ' S.insert (it, 3, ' a '), cout << Endl << "after insert:\n";//The values of all elements are printed out by Iterators for (It=s.begin (); It!=s.end (); it++) {cout<< *it << ",";} cout << Endl;reTurn 0;}
The results of the operation are as follows:
This tutorial sample code download Please click this link: http://download.csdn.net/detail/tennysonsky/8896441
Copyright NOTICE: This blog post, mostly I compiled, or in the network collection, reproduced please indicate the source!!
Getting Started with C + +-using the standard library string class