STL string是真正的字符串。
#include <iostream>
#include <string>
#include <algorithm>
int main()
{
char pszName[20] = "qwer";//C语言的字符串
std::string strName("qwer");//C++语言中的字符串
std::cout << pszName << std::endl;
std::cout << strName << std::endl;
const char * oszC = "Hello String";
std::string strC(oszC);//把C语言风格的字符串转变为C++风格的字符串
std::string strC1(strC);//把strC的值复制到strC1中。
std::string strC2(strC,1);//把strC的值复制到strC1中。去掉第一个值2
std::string str3(10,'a');//初始化10个a
std::cout << oszC << std::endl;
std::cout << strC << std::endl;
std::cout << strC1 << std::endl;
std::cout << strC2 << std::endl;
}
C语言风格的复制。
const char* pasConstStr = "hello C";
char *pasCopy = new char(strlen(pasConstStr)+1);
strcopy(pasCopy,pasConstStr);
delete[] pasCopy;
c++字符串输出
#include<iostream>
#include<string>
#include<vector>
#include<map>
using namespace std;
int main()
{
string strSTLString("hello string");
cout << "使用传统的方法显示字符串" << endl;
for (size_t nCharCounter = 0;
nCharCounter < strSTLString.length(); //求出字符串的个数
++nCharCounter)
{
cout << strSTLString[nCharCounter] << endl;
}
cout << strSTLString << endl;//这样可以全部输出
cout << "使用迭代器来操作字符串中的每一个字符" << endl;
string::const_iterator itr;
for (itr = strSTLString.begin();//begin()是一个方法
itr != strSTLString.end();
++itr)//迭代器是一个指针
{
cout << *itr << endl;
}
cout << "使用新的for循环格式循环输出" << endl;
for (auto x : strSTLString)
{
cout << x << endl;
}
cout << "把c++的字符串变成c风格的字符串" << endl;
cout << strSTLString.c_str() << endl;//把c++的字符串变成c风格的字符串
return 0;
}
C++字符串拼接
#include<iostream>
#include<string>
#include<vector>
#include<map>
using namespace std;
int main()
{
string strSample1("hello");
string strSample2("String");
cout << "使用+=拼接字符串" << endl;
strSample1 += strSample2;
cout << strSample1 << endl;
cout << "使用append拼接字符串" << endl;
string strSampla3("Fun is not needing to use pointers!");
strSample1.append(strSampla3);
cout << strSample1 << endl;
//使用append可以拼接C风格的字符串
cout << "使用append拼接C风格的字符串" << endl;
const char * pszConstString = "You however stil can!";
strSample1.append(pszConstString);
cout << strSample1 << endl;
return 0;
}
C++字符串的查找
#include<iostream>
#include<string>
#include<vector>
#include<map>
using namespace std;
int main()
{
string strSample("Good day String! Today is beautiful!");
cout << strSample << endl;
//find()方法,查找方法,第一个参数是查找的字符串,第二个参数是从位置开始找
//这个方法只能找到第一个day的位置,找到就返回npos,不会找第二个day
size_t nOffset = strSample.find("day",0);
//关于npos的解释,没找到就会返回npos,本质就是-1,没有找到nops就找到了
if (nOffset != string::npos)
{
cout << "在下标:" << nOffset << "找到day!" << endl;
}
else
{
cout << "没找到!" << endl;
}
//找到全部的day字符串
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 << "查找字符" << endl;
size_t nSearchOffset = strSample.find('a', 0);
while (nSearchOffset != string::npos)
{
cout << "在下标:" << nSearchOffset << "找到a!" << endl;
size_t wnSearchOffset = nSearchOffset + 1;
nSearchOffset = strSample.find('a', wnSearchOffset);
}
return 0;
}
c++字符串截短
#include<iostream>
#include <algorithm>
#include<string>
#include<vector>
#include<map>
using namespace std;
int main()
{
string strSample("Hello String Wake up to a beautiful day!");
cout << strSample << endl;
//对字符串进行裁剪,13到28都不要了
strSample.erase(13,28);
cout << strSample << endl;
//通过迭代器,和find算法,找到S字符并且删除,find算法,没有第三个参数就是查找全部
string::iterator iChars = find(strSample.begin(),strSample.end(),'S');
if (iChars != strSample.end())
{
strSample.erase(iChars);
}
cout << strSample << endl;
return 0;
}
字符串算法,字符串翻转,大小写转换
#include<iostream>
#include <algorithm>
#include<string>
#include<vector>
#include<map>
using namespace std;
int main()
{
string strSample("Hello string We will revrse you!");
cout << strSample << endl;
//字符串翻转
reverse(strSample.begin(),strSample.end());
cout << strSample << endl;
//大小写转换
cout << "请输入一行字符串:" << endl;
string strInput;
getline(cin,strInput);
//第一个参数就是转换位置的开头,第二个参数是转换位置的结尾,
//第三个参数是转换后需要放的位置,第四个参数是小写转换大写toupper,大写转小写towlower
transform(strInput.begin(),strInput.end(),strInput.begin(),toupper);
cout << strInput << endl;
transform(strInput.begin(), strInput.end(), strInput.begin(), towlower);
cout << strInput << endl;
return 0;
}