#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
//string strSTLString("Hello String");
//for (size_t nCharCounter = 0; nCharCounter < strSTLString.length(); ++nCharCounter)
//{
// cout << "Character[" << nCharCounter << "] is :";
// cout << strSTLString[nCharCounter] << endl;
//}
//cout << endl;
//int charOffset = 0;
//string::const_iterator iCharacterLocator;
//for (iCharacterLocator = strSTLString.begin();
//iCharacterLocator != strSTLString.end(); ++iCharacterLocator)
//{
// cout << "Character[" << charOffset++ << "]is:";
// cout << *iCharacterLocator << endl;
//}
//cout << endl;
//cout << "the char* representation of the string is :";
//cout << strSTLString.c_str() << endl;
//string strSample1("Hello");
//string strSample2("String");
//strSample1 += strSample2;
//cout << strSample1 << endl;
//string strSample3("Fun is not needing to use pointers");
//strSample1.append(strSample3);
//cout << strSample1 << endl;
//const char* constCStyleString = "you however still can!";
//strSample1.append(constCStyleString);
//cout << strSample1 << endl;
//string strSample("God day String Today is beadayutiful");
//cout << "the sample string is " << endl;
//cout << strSample << endl;
//size_t charPos = strSample.find("day", 0);
//if (charPos != string::npos)
// cout << "First instance of day was found at position" << charPos;
//else
// cout << "Substring not foud" << endl;
//cout << endl;
//cout << "locating all instance of substring day" << endl;
//size_t SubstringPos = strSample.find("day", 0);
//while (SubstringPos != string::npos)
//{
// cout << "day found at position" << SubstringPos << endl;
// size_t nSearchPosition = SubstringPos + 1;
// SubstringPos = strSample.find("day", nSearchPosition);
//}
//cout << "Locating all instance of character'a'" << endl;
//const char charToSearch = 'a';
//charPos = strSample.find(charToSearch, 0);
//while (charPos != string::npos)
//{
// cout << "'" << charToSearch << "'found";
// cout << " at position:" << charPos << endl;
// size_t charSearchPos = charPos + 1;
// charPos = strSample.find(charToSearch, charSearchPos);
//}
//cout << endl;
string strSample("Hello String !Wake up to a beautiful day!");
cout << "the original sample string is " << endl;
cout << strSample << endl;
cout << "Truncating the second sentence:" << endl;
strSample.erase(13, 28);
cout << strSample << endl;
string::iterator iCharS = find(strSample.begin(), strSample.end(), 'S');
cout << "Ereaseing character 'S' from the sample string:" << endl;
if (iCharS != strSample.end())
strSample.erase(iCharS);
cout << strSample << endl;
cout << "Erasing a range between begin() and end():" << endl;
strSample.erase(strSample.begin(), strSample.end());
if (strSample.length() == 0)
cout << "the string is empty" << endl;
system("pause");
return 0;
}
基于C++的字符操作 string
最新推荐文章于 2024-04-21 09:47:46 发布