My Training Period: xx hours
This is a continuation from previous Module, compiled usingVisual C++ 6.0 with SP6 and Visual C++ .Net. Linux g++ (GNU C++ run on my Fedora 3 machine) example is given at the end of this Module. Take note also for the codes that span more than one line, which they are not supposed to. The source code for this tutorial is available atC++ Characters & Strings source codes.
data(), length(), strlen() and c_str() example
empty() example
begin(), end() example
erase() example
find() example
find_first_not_of() example
find_first_of() example
example compiled usingg++
|
|
data()
// data(), length(), strlen() and c_str() #include <string> #include <iostream> using namespace std;
int main() { string str1("Testing the data()"); cout<<"str1 string object is: "<<str1<<endl; cout<<"Operation: str1.length()"<<endl; cout<<"The length of str1 = "<<unsigned int(str1.length())<<"\n\n"; // converting a string to an array of characters const char *ptr1 = 0; ptr1= str1.data(); cout<<"Operation: str1.data()"<<endl; cout<<"The modified ptr1 string object is: "<<ptr1<<endl; cout<<"Operation: strlen(ptr1)"<<endl; cout<<"The length of character array str1 = "<<unsigned int(strlen(ptr1))<<"\n\n"; // converting a string to a C-style string const char *cstr1 = str1.c_str(); cout<<"Operation: str1.c_str()"<<endl; cout<<"The C-style string c_str1 is: "<<cstr1<<endl; cout<<"Operation: strlen(ptr1)"<<endl; cout<<"The length of C-style string str1 = "<<unsigned int(strlen(cstr1))<<endl; return 0; }
|

The return value is true if the string object contains no characters; false if it has at least one character.
Theempty() is equivalent tosize == 0.
// empty()
#include <string>
#include <iostream>
using namespace std;
int main()
{
bool Var1, Var2;
string str1("Testing the empty()");
cout<<"str1 string object is: "<<str1<<endl;
Var1 = str1.empty();
// test the emptiness
cout<<"Operation: str1.empty()"<<endl;
if(Var1)
cout<<"str1 string object is empty."<<endl;
else
cout<<"str1 string object is not empty."<<"\n\n";
// an example of an empty string object
string str3;
Var2 = str3.empty();
cout<<"Operation: str3.empty()"<<endl;
// test the emptiness
if(Var2)
cout<<"str3 string object is empty."<<endl;
else
cout<<"str3 string object is not empty."<<endl;
return 0;
}

The return value is a random-access iterator that addresses the location succeeding the last element in a string.
end() is often used to test whether an iterator has reached the end of its string. The value returned byend() should not be dereferenced.
If the return value of end() is assigned to aconst_iterator, the string object cannot be modified. If the return value of end() is assigned to an iterator, the string object can be modified.
// begin(), end()
#include <string>
#include <iostream>
using namespace std;
int main( )
{
string str1("Testing the end()");
basic_string <char>::iterator StrIter, Str1Iter;
Str1Iter = str1.end();
// minus the null character, so point to the real last character in the string...
Str1Iter--;
cout<<"Operation: str1.end() then Str1Iter--"<<endl;
cout<<"str1 string is: "<<str1<<endl;
cout<<"The last character of the str1 string is: "<<*Str1Iter<<endl;
// end() used to test when an iterator has reached the end of its string
cout<<"Using forward iterator the str1 is: ";
for(StrIter = str1.begin(); StrIter != str1.end(); StrIter++)
cout<<*StrIter;
cout<<"\n\n";
// the dereferenced iterator can be used to modify a character
// the last event, this pointer point to the last character in the string
*Str1Iter = 'F';
cout<<"Operation: *Str1Iter = 'F'"<<endl;
cout<<"Now, the last character of the modified str1 is: "<<*Str1Iter<<endl;
cout<<"Then the modified str1 string is: "<<str1<<endl;
return 0;
}

erase()
The return value: For the first two member functions, an iterator addressing the first character after the last character removed by the member function. For the third member function, a reference to the string object from which the elements have been erased.
The third member function returns *this.
// erase()
#include <string>
#include <iostream>
using namespace std;
int main()
{
// using a range...
string str1("Testing the erase() part I");
basic_string <char>::iterator Str1Iter;
cout<<"str1 string object is: "<< str1<<endl;
// don't forget the null character
Str1Iter = str1.erase(str1.begin() + 4, str1.end() - 6);
cout<<"Operation: str1.erase(str1.begin()+4, str1.end()-6)"<<endl;
cout<<"The first element after those removed is: "<<*Str1Iter<<endl;
cout<<"The modified str1 string object is: "<<str1<<endl;
// erasing a char pointed to by an iterator
string str2("Testing the erase() part II");
basic_string <char>::iterator Str2Iter;
cout<<"\nstr2 string object is: "<<str2<<endl;
Str2Iter = str2.erase(str2.begin() + 3);
cout<<"Operation: str2.erase(str2.begin() + 3)"<<endl;
cout<<"The first element after those removed is: "<<*Str2Iter<<endl;
cout<<"The modified str2 string object is: \n"<<str2<<endl;
// erasing a number of chars after a char
string str3("Testing the erase() part III"), NewStr3;
cout<<"\nThe original string object str3 is: \n"<<str3<<endl;
NewStr3 = str3.erase(6, 8);
cout<<"Operation: str3.erase(6, 6)"<<endl;
cout<<"The modified NewStr3 string object is: \n"<<NewStr3<<endl;
return 0;
}

The return value is the index of the first character of the substring searched for when successful; otherwise npos.

-------------------------------------------------------------------------------------------

The return value is the index of the first character of the substring searched for when successful; otherwise npos.
// find_first_not_of() part I
#include <string>
#include <iostream>
using namespace std;
int main()
{
// searching a single character in a string
string str1("Testing the find_first_not_of() part 1");
cout<<"str1 string is: "<<str1<<endl;
basic_string <char>::size_type index1, index2;
static const basic_string <char>::size_type npos = -1;
index1 = str1.find_first_not_of('_', 3);
cout<<"Operation: str1.find_first_not_of('_', 3)"<<endl;
if(index1 != npos)
cout<<"The index of the 1st '_' found after the 3rd\nposition in str1 is: "<<unsigned int(index1)<<endl;
else
cout<<"The character '_' was not found in str1"<<endl;
index2 = str1.find_first_not_of('T');
cout<<"\nOperation: str1.find_first_not_of('T')"<<endl;
if(index2 != npos)
cout<<"The index of the 'non T' found in str1 is: "<<unsigned int(index2)<<endl;
else
cout<<"The character 'non T' was not found in str1."<<endl;
cout<<endl;
// ---------------------------------------------------------------------------
// searching a string for a substring as specified by a C-string
string str2("Testing the find_first_not_of() part 2");
cout<<"str2 string is: "<<str2<<endl;
basic_string <char>::size_type index3, index4;
const char *cstr2 = "df";
index3 = str2.find_first_not_of(cstr2, 4);
cout<<"Operation: str2.find_first_not_of(cstr2, 4)"<<endl;
if(index3 != npos)
cout<<"The index of the 1st occurrence of an element\nof 'df' in str2 after the 4th "
<<"position is: "<<unsigned int(index3)<<endl;
else
cout<<"Elements of the substring 'df' were not\nfound in str2 after the 4th position."<<endl;
const char *cstr3 = "gz";
index4 = str2.find_first_not_of(cstr3);
cout<<"\nOperation: str2.find_first_not_of(cstr3)"<<endl;
if(index4 != npos)
cout<<"The index of the 1st element of 'gz' after\nthe 0th position in str2 is: <<unsigned int(index4)<<endl;
else
cout<<"The substring 'gz' was not found in str2"<<endl;
return 0;
}
Output:

// find_first_not_of() part II
#include <string>
#include <iostream>
using namespace std;
int main()
{
// searching a string for a substring as specified by a C-string
string str3("Testing the find_first_not_of() part 3");
cout<<"str3 string is: "<<str3<<endl;
basic_string <char>::size_type index5, index6;
static const basic_string <char>::size_type npos = -1;
const char *cstr4 = "nro";
index5 = str3.find_first_not_of(cstr4);
cout<<"Operation: str3.find_first_not_of(cstr4)"<<endl;
if(index5 != npos)
cout<<"The index of the 1st occurrence of an element in str3\nother than one of the "
<<"characters in 'nro' is: "<<unsigned int(index5)<<endl;
else
cout<<"Elements in str3 contain only characters in the string 'nro'. "<<endl;
const char *cstr5 = "nro";
index6 = str3.find_first_not_of(cstr5, index5+1, 2);
cout<<"\nOperation: str3.find_first_not_of(cstr5, index5+1, 2)"<<endl;
if(index6 != npos)
cout<<"The index of the second occurrence of an element\nof 'nro' in str3 after the 0th "
<<"position is: "<<unsigned int(index6)<<endl<<endl;
else
cout<<"Elements in str3 contain only characters in the string 'nro'"<<endl;
cout<<endl;
// --------------------------------------------------------------------
// searching a string for a substring as specified by a string
string str4("Testing the find_first_not_of() part 4");
cout<<"str4 string is: "<<str4<<endl;
basic_string <char>::size_type index7, index8;
string str5("tf7");
index7 = str4.find_first_not_of(str5, 3);
cout<<"Operation: str4.find_first_not_of(str5, 3)"<<endl;
if(index7 != npos)
cout<<"The index of the 1st non occurrence of an element\nof 'tf7' "
<<"in str4 after the 3rd position is: "<<unsigned int(index7)<<endl;
else
cout<<"Elements other than those in the substring 'tf7' were not found in the string str4."<<endl;
string str6("in");
index8 = str4.find_first_not_of(str6);
cout<<"\nOperation: str4.find_first_not_of(str6)"<<endl;
if(index8 != npos)
cout<<"The index of the 1st occurrence of an element of\n'in' in str4 after the 0th "
<<"position is: "<<unsigned int(index8)<<endl;
else
cout<<"Elements other than those in the substring 'in' were not found in the string str4."<<endl;
return 0;
}

The return value is the index of the first character of the substring searched for when successful; otherwise npos.
// find_first_of() part I
#include <string>
#include <iostream>
using namespace std;
int main( )
{
// searching for a single character in a string
string str1("find_first_of()");
cout<<"str1 string is: "<<str1<<endl;
basic_string <char>::size_type index1, index2;
static const basic_string <char>::size_type npos = -1;
index1 = str1.find_first_of('r', 3);
cout<<"Operation: str1.find_first_of('r', 3)"<<endl;
if(index1 != npos)
cout<<"The index of the 1st 'r' found after the 3rd\n"
<<"position in str1 is: "<<unsigned int(index1)<<endl;
else
cout<<"The character 'r' was not found in str1"<<endl;
index2 = str1.find_first_of('z');
cout<<"\nOperation: str1.find_first_of('z')"<<endl;
if(index2 != npos)
cout<<"The index of the 'z' found in str1 is: "<<unsigned int(index2)<<endl;
else
cout<<"The character 'z' was not found in str1."<<endl;
// --------------------------------------------------------
// searching a string for a substring as specified by a C-string
string str2("Testing 123...Testing 123");
cout<<"\nstr2 string is: "<<str2<<endl;
basic_string <char>::size_type index3, index4;
const char *cstr = "s1";
index3 = str2.find_first_of(cstr, 3);
cout<<"Operation: str2.find_first_of(cstr, 3)"<<endl;
if(index3 != npos)
cout<<"The index of the 1st occurrence of an element\nof 's1' in str2 after the 3rd "
<<"position is: "<<unsigned int(index3)<<endl;
else
cout<<"Elements of the substring 's1' were not\nfound in str2 after the 3rd position."<<endl;
const char *cstr1 = "g3";
index4 = str2.find_first_of(cstr1);
cout<<"\nOperation: str2.find_first_of(cstr1)"<<endl;
if(index4 != npos)
cout <<"The index of the 1st element of 'g3'\nafter the 0th position in str2 is: "<<unsigned int(index4)<<endl;
else
cout<<"The substring 'g3' was not found in str2."<<endl;
return 0;
}

// find_first_of() part II
#include <string>
#include <iostream>
using namespace std;
int main( )
{
// searching a string for a substring as specified by a C-string
string str3("Testing 456...Testing 456...789");
cout<<"str3 string is: "<<str3<<endl;
basic_string <char>::size_type index5, index6;
static const basic_string <char>::size_type npos = -1;
const char *cstr2 = "t6";
index5 = str3.find_first_of(cstr2);
cout<<"Operation: str3.find_first_of(cstr2)"<<endl;
if(index5 != npos)
cout<<"The index of the 1st occurrence of an element\nof 't6' in str3 after the 0th "
<<"position is: "<<unsigned int(index5)<<endl;
else
cout<<"Elements of the substring 't6' were not\nfound in str3 after the 0th position."<<endl;
const char *cstr3 = "t68";
index6 = str3.find_first_of(cstr3, index5 + 1, 2);
cout<<"\nOperation: str3.find_first_of(cstr3, index5 + 1, 2)"<<endl;
if(index6 != npos)
cout<<"The index of the second occurrence of an element\nof 't68' in str3 after the 0th "
<<"position is: "<<unsigned int(index6)<<endl;
else
cout<<"Elements of the substring 't68' were not\nfound in str3 after the first occurrence."<<endl;
cout<<endl;
//------------------------------------------------------------
//searching a string for a substring as specified by a string
string str4("find_first_of() and find_first_of()");
cout<<"str4 string is: "<<str4<<endl;
basic_string <char>::size_type index7, index8;
string str5("dfz");
index7 = str5.find_first_of(str5, 3);
cout<<"Operation: str5.find_first_of(str5, 3)"<<endl;
if(index7 != npos)
cout<<"The index of the 1st occurrence of an element\nof 'dfz' in str4 after the 3rd "
<<"position is: "<<unsigned int(index7)<<endl;
else
cout<<"Elements of the substring 'dfz' were not\n"
<<"found in str4 after the 3rd position."<<endl;
string str6("fo");
index8 = str4.find_first_of(str6);
cout<<"\nOperation: str4.find_first_of(str6)"<<endl;
if(index8 != npos)
cout<<"The index of the 1st occurrence of an element\nof 'fo' in str4 after the 0th "
<<"position is: "<<unsigned int(index8)<<endl;
else
cout<<"Elements of the substring 'fo' were not\nfound in str4 after the 0th position."<<endl;
return 0;
}

To be continued on next Module :o).
For C’s character and string manipulations please refer toModuleX.
The following is a program example compiled usingg++.
// **********string.cpp**************
// append()
#include <string>
#include <iostream>
using namespace std;
int main()
{
// appending a C-string to a string
string str1("Playing ");
const char *str2 = "with a string";
cout<<"str1 is: "<<str1<<endl;
cout<<"str2, C string is: "<<str2<<endl;
str1.append(str2);
cout<<"Operation: str1.append(str2)"<<endl;
cout<<"Appending str2 to str1: "<<str1<<endl;
// appending part of a C-string to a string
string str3 ("Replaying ");
const char *str4 = "the string ";
cout<<"\nstr3 string is: "<<str3<<endl;
cout<<"str4 C-string is: "<<str4<<endl;
str3.append(str4, 6);
cout<<"Operation: str3.append(str4, 6)"<<endl;
cout<<"Appending part of the str4 to string str3: \n"<<str3<<endl;
// appending part of one string to another
string str5("Again "), str6("string manipulation");
cout<<"\nstr5 is: "<<str5<<endl;
cout<<"str6 is: "<<str6<<endl;
str5.append(str6, 4, 6);
cout<<"Operation: str5.append(str6, 4, 6)"<<endl;
cout<<"The appended string is: "<<str5<<endl;
// appending one string to another in two ways,
// comparing append and operator [ ]
string str7("First "), str8("Second "), str9("Third ");
cout<<"\nstr7 is: "<<str7<<"\nstr8 is: "<<str8<<"\nstr9 is: "<<str9<<endl;
str7.append(str8);
cout<<"Operation: str7.append(str8)"<<endl;
cout<<"The appended string str7 is: "<<str7<<endl;
str7 += str9;
cout<<"Operation: str7 += str9"<<endl;
cout<<"The re appended string is: "<<str7<<endl;
// appending characters to a string
string str10("What string");
cout<<"\nstr10 string is: "<<str10<<endl;
str10.append(3, '?');
cout<<"Operation: str10.append(3, '?')"<<endl;
cout<<"str10 string appended with ? is: "<<str10<<endl;
// appending a range of one string to another
string str11("Finally "), str12("comes the END ");
cout<<"\nstr11 is: "<<str11<<" str12 is: "<<str12<<endl;
str11.append(str12.begin() + 6, str12.end() - 1);
cout<<"Operation:\nstr11.append(str12.begin() + 6, str12.end() - 1)"<<endl;
cout<<"The appended str11 String is: "<<str11<<endl;
return 0;
}
[bodo@bakawali ~]$ g++ string.cpp -o stringapp
[bodo@bakawali ~]$ ./stringapp
str1 is: Playing
str2, C string is: with a string
Operation: str1.append(str2)
Appending str2 to str1: Playing with a string
str3 string is: Replaying
str4 C-string is: the string
Operation: str3.append(str4, 6)
Appending part of the str4 to string str3:
Replaying the st
str5 is: Again
str6 is: string manipulation
Operation: str5.append(str6, 4, 6)
The appended string is: Again ng man
str7 is: First
str8 is: Second
str9 is: Third
Operation: str7.append(str8)
The appended string str7 is: First Second
Operation: str7 += str9
The re appended string is: First Second Third
str10 string is: What string
Operation: str10.append(3, '?')
str10 string appended with ? is: What string???
str11 is: Finally str12 is: comes the END
Operation:
str11.append(str12.begin() + 6, str12.end() - 1)
The appended str11 String is: Finally the END
tenouk C++ STL string class programming tutorial
Further C++ string related reading:
Acomplete C++ Standard Library documentation that includes STL.
Check thebest selling C / C++, STL and UML books at Amazon.com.
The source code for this tutorial is available atC++ Characters & Strings source codes.