实例化和复制STL string类:
#include <string>
#include <iostream>
int main()
{
using namespace std;
const char* mystring = "hello string"; //C风格的字符串
cout<<"Constant string is : "<<mystring<<endl;
std::string strFromConst(mystring); //构造字符串类型的对象
cout<<"Constant string is : "<<strFromConst<<endl;
std::string str2("hello string.");
std::string str2Copy(str2); //用一个对象去拷贝构造另一个对象
cout<<"str2Copy is : "<<str2Copy<<endl;
std::string strPartialCopy(mystring, 5); //部分构造的方式
cout<<"strPartialCopy is : "<<strPartialCopy<<endl;
std::string startRepeatChars(10, 'a'); //重复构造的方式
cout<<"startRepeatChars is : "<<startRepeatChars<<endl;
return 0;
}
访问std::string 的字符串内容:
#include <string>
#include <iostream>
int main()
{
using namespace std;
//const char* mystring = "hello string";
std::string mystring("hello string");
//用数组下标的方式访问字符串类的每一个元素
cout<<"Display elements in string using array-syntax: "<<endl;
for(size_t i = 0;i < mystring.length();++i)
{
cout<<"Character ["<<i<<"] is:";
cout<<mystring[i]<<endl;
}
cout<<endl;
//迭代器的方式来访问字符串类中的每一个元素
cout<<"Display elements in string using iterators: "<<endl;
int charoffset = 0;
string::const_iterator charLocator;
for(charLocator =mystring.begin();charLocator != mystring.end();++charLocator)
{
cout<<"Character ["<<charoffset<<"] is : ";
cout<<*charLocator<<endl;
}
cout<<endl;
cout<<"char* string is : ";
cout<<mystring.c_str()<<endl;
return 0;
}
我用的CB的版本还不支持auto关键字,string类中也没有.cbegin()方法,为了使程序能够跑起来,对程序做了一些修改,然后运行程序。
拼接字符串:
#include <string>
#include <iostream>
int main()
{
using namespace std;
string str1("hello");
string str2(" string");
//用+=的方式拼接字符串
str1 += str2;
cout<< str1<<endl<<endl;
//用append()方法拼接字符串
string str3(" Fun is not needing to use pointers!");
str1.append(str3);
cout<< str1<<endl<<endl;
//常字符串也可以作为append方法的参数
const char* cstr = " You however still can!" ;
str1.append(cstr);
cout<< str1<< endl;
return 0;
}
在string中查找字符或子字符串:
#include <string>
#include <iostream>
int main()
{
using namespace std;
string str("Good day String! Today is beautiful!");
cout<<"str is :"<<endl<<str<<endl<<endl;
size_t charPos = str.find("day", 0);
if(charPos != string::npos) //find返回不等于string::npos时,说明找到了
cout<< "First instance \"day\" at pos. "<<charPos <<endl;
else
cout<<"Substring not found"<<endl;
cout<<"Locate all instances of substring \"day\""<<endl;
size_t subStrPos = str.find("day", 0);
//查找string中的指定字符串或是子字符串的所有实例
while(subStrPos != string::npos)
{
cout<< "\"day\" found at position "<< subStrPos << endl;
size_t searchOffset = subStrPos + 1;//找到的话重新设置偏移量,开始搜索下一个子字符串
subStrPos = str.find("day",searchOffset);
}
return 0;
}
截短STL string:
#include <string>
#include <iostream>
#include <algorithm>
int main()
{
using namespace std;
string str("Hello String! Wake up to a beautiful day!");
cout << "The original sample string is : " << endl;
cout << str << endl << endl;
str.erase(13, 28);//给定偏移位置和字符数,删除指定数目的字符
cout << str << endl << endl;
string::iterator it = find(str.begin(),str.end(),'S');//找到字符‘S’,并返回迭代器
if(it != str.end())//删除迭代器所指向的字符
str.erase(it);
cout << str << endl << endl;
str.erase(str.begin(), str.end());//删除由两个迭代器指定范围内的字符
if(str.length() == 0)
cout << "The string is empty" << endl;
return 0;
}
在写代码的时候,忘记加算法头文件了,导致编译有问题,真的是太二了。
字符串反转:
#include <string>
#include <iostream>
#include <algorithm>
int main()
{
using namespace std;
string str("Hello String! We will reverse you!");
cout<<"The original string is : "<<endl;
cout<<str<<endl<<endl;
reverse (str.begin(), str.end());
cout<<"After reverse : "<<endl;
cout << str << endl;
return 0;
}
这个反转的方法还真的是挺好用的,不错啊。
字符串的大小写转换:
#include <string>
#include <iostream>
#include <algorithm>
int main()
{
using namespace std;
cout<<"input a string:"<<endl;
cout<<"> ";
string str;
getline(cin, str);
cout<<endl;
transform(str.begin(), str.end(), str.begin(), ::toupper);//字符串大小写,转换方法
cout<<str<<endl<<endl;
transform(str.begin(), str.end(), str.begin(), ::tolower);
cout<<str<<endl<<endl;
return 0;
}
std::string类实际上是std::basic_string<T>的具体化。注意:如果要编写的应用程序需要更好的支持非拉丁字符,如中文和日文,应使用std::wstring类。
C++14中新增operator""s,使得实例能够包含并操作含有空字符的字符缓冲区。