基于C++的字符操作 string

#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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值