/*
STL string c++字符串
*/
#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
#include <tchar.h>
int main()
{
char name[20]; // C语言 的字符串
char *name1 = "dskfdsf"; // // C语言 的字符串
// c++字符串
//using namespace std;
//std::string
string str("你是");
string str1 = "dfsdf";
const char* lala = "dsfsdf!";
std::string ss(lala);
std::string strPartialCopy(lala, 5);
std::string strRepeatChars(10, 'a');
const char *pszConstStr = "Hello C";
char *pszCopy = new char(strlen(pszConstStr) + 1);
strcpy(pszCopy, pszConstStr);
cout<<pszCopy<<endl;
//delete[] pszCopy;
return 0;
}
<span style="color:#ff0000;">字符串连接</span>
<pre name="code" class="cpp">#include <iostream>
//
#include <string>
using namespace std;
int main()
{
cout << "请输入一行字符串!" << endl;
string strSample1("Hello");
string strSample2("String!");
strSample1 += strSample2;
string strSample3("Fun is not needing to use pointers");
strSample1.append(strSample3);
const char *pszConstString = "You however still can!"; // c语言风格的字符串
strSample1.append(pszConstString);
system("pause");
return 0;
}
字符串反转
#include <iostream>
//
#include <string>
#include <algorithm>
using namespace std;
int main()
{
string strSample("Hello String! We will reverse you!");
cout << strSample << endl;
reverse(strSample.begin(), strSample.end());
cout << strSample << endl;
system("pause");
return 0;
}
截取
#include <iostream>
//
#include <string>
#include <algorithm>
using namespace std;
int main()
{
string strSample("Hello String! Wake up to a beautiful day!");
cout << strSample << endl;
// erase
strSample.erase(13, 28);
cout << strSample << endl;
// find 算法,迭代器
string::iterator iCharS = find(strSample.begin(), strSample.end(), 'S');
if(iCharS != strSample.end())
{
strSample.erase(iCharS);
}
cout << strSample << endl;
strSample.erase(strSample.begin(), strSample.end());
if(strSample.length() == 0)
{
cout << "The string is empty!" << endl;
}
system("pause");
return 0;
}
迭代
#include <iostream>
#include <string>
//
using namespace std;
int main()
{
string strSTLString("Hello String");
for(size_t i = 0; i < strSTLString.length();++i)
{
cout<<strSTLString[i] << endl;
}
//
cout << "使用STL里面的迭代器!" << endl;
string::const_iterator itr;
for(itr = strSTLString.begin(); itr != strSTLString.end(); ++itr)
{
cout << *itr << endl; // 迭代器是一个指针
}
system("pause");
return 0;
}
查找
#include <iostream>
#include <string>
//
using namespace std;
int main()
{
string strSample("Good day String! Today is beautiful!");
cout << strSample << endl;
size_t nOffset = strSample.find("day", 0); // 从0 开始找
if(nOffset != string::npos) // npos 实际上是-1
{
cout << "在下标:" << nOffset << "找到了!" << endl;
}
else
{
cout << "没有找到!" << endl;
}
// 查找所有的day
cout << "\n\n查找所有的day:"<< endl;
size_t nSubstringOffset = strSample.find("day", 0);
while(nSubstringOffset != string::npos)
{
cout << "在下标这里:" << nSubstringOffset << "找到day!"<< endl;
size_t nSearchOffset = nSubstringOffset + 1;
nSubstringOffset = strSample.find("day", nSearchOffset);
}
//
cout << "\n\n查找所有的字符a:" << endl;
size_t nCharacterOffset = strSample.find('a', 0);
while(nCharacterOffset != string::npos)
{
cout << "在下标这个:" << nCharacterOffset << "找到a!" << endl;
size_t nCharSearchOffset = nCharacterOffset + 1;
nCharacterOffset = strSample.find('a', nCharSearchOffset);
}
system("pause");
return 0;
}