1. 截取子字符串:
string string1partcopy=string1.substr(size_t beginindex, size_t length);
子字符串是从元字符串的索引(从0开始)为beginindex处开始的长度为length的字符串,即:string1【beginindex】为第一个被复制的字符串,若beginindex=string1.size(),则返回一个空字符串。当length>string1.size()或将length写成string::npos时,则等价于全复制.(注:想全复制时直接用string string2=string1,最简洁)
示例程序:
#include<iostream>
#include<string>
usingnamespace std;
intmain()
{
string s="abcdefgh";
string spartcopy=s.substr(1,3);
cout<<spartcopy<<endl;
spartcopy=s.substr(s.length(),3);
cout<<spartcopy<<"标志"<<endl;
string scopy=s.substr(0,100);
cout<<scopy<<endl;
scopy=s.substr(0,string::npos);
cout<<scopy<<endl;
scopy=s;
cout<<scopy<<endl;
return 0;
}
运行结果:
bcd
标志
abcdefgh
abcdefgh
abcdefgh
1. 获取字符串的长度:int changdu=string1.size()或int changdu=string1.length()
2. 迭代器:stringname.begin()返回指向开头的迭代器,* stringname.begin()为开头第一个字符(注意:stringname[i]是字符‘a’,不是字符串“a”) stringname.end()为字符串中最后一个字符的下一个,看不见,*(stringname.end()-1)为最后一个字符。string类的迭代器支持+ -运算。如:string1=”abcdefg”,cout<<*(s.begin()+3)输出:d.
示例程序:
#include<iostream>
#include<string>
usingnamespace std;
intmain()
{
string s="abcdefgh";
cout<<*(s.begin())<<endl;
cout<<*(s.begin()+3)<<endl;
cout<<*(s.end())<<"标志"<<endl;
cout<<*(s.end()-1)<<endl;
return 0;
}
运行结果:
a
d
标志
h
其他不常用的迭代器如下:
Return reverse iterator to reversebeginning (public member function )
Return reverse iterator to reverse end (public memberfunction )
Return const_iterator to beginning (public memberfunction )
Return const_iterator to end (public memberfunction )
Return const_reverse_iterator to reversebeginning (public member function )
Return const_reverse_iterator to reverseend (public member function )
3. 重新指定字符串的大小:stringname.resize(size_t newlength) 或
stringname.resize(size_t newlength, char c) 说明:两种操作均返回void,也就是两种方法均直接对原字符串进行修改。当newlength<=stringname.length()时变成原字符串的前newlength个字符,这时char c有没有的结果一样,即使加上也不起作用;当newlength>stringname.length()时,字符串会延长到newlength的长度,若char c不填,则在原来的字符串后面补充空白符:’ ’,填上char c后,,则在原来的字符串后面补充c。
示例程序:
#include<iostream>
#include<string>
usingnamespace std;
intmain()
{
string s="abcde";
cout<<s.length()<<endl;
s.resize(3,'y');
cout<<s.length()<<"\t"<<s<<endl;
s.resize(10);
cout<<s.length()<<"\t"<<s<<"END"<<endl;
s.resize(13,'X');
cout<<s.length()<<"\t"<<s<<"END"<<endl;
}
运行结果:
5
3 abc
10 abc END
13 abc XXXEND
4. 清空字符串的内容:stringname.clear();该方法返回void
示例程序:
#include<iostream>
#include<string>
usingnamespace std;
intmain()
{
string s="aaaaaaaaaaaaaaaaa";
cout<<s<<endl;
s.clear();//相当于s=””;什么都不输出。
cout<<s<<s.length();
return 0;
}
运行结果:
aaaaaaaaaaaaaaaaa
0
5. 得到字符串开头(末尾)的字符值:char ch=stringname.front()(char ch=stringname.back())…stringname.front()方法返回字符串开头字符的引用,若该字符串不是const常量字符串则可以通过返回的引用修改开头的值,否则不能修改。stringname.back()用法同上。
示例程序:
#include<iostream>
#include<string>
usingnamespace std;
intmain()
{
string s="abcdefg";
char c1=s.front();
cout<<c1<<endl;
s.front()='A';
cout<<s<<endl;
char c2=s.back();
cout<<c2<<endl;
s.back()='G';
cout<<s;
return 0;
}
运行结果:
a
Abcdefg
g
AbcdefG
6. 将一个字符串(string2)的某部分(string2[x]---string2[x+n],x>=0,共n+1个字符)连到另一个字符串(string1)的尾部:法一:string1.append(string2,x,n+1)(从string2[x]开始复制n+1个字符) 法二:string1.append(string2.begin()+x,string2.begin()+x+n+1)(从string2.begin()+x开始复制,不包括string2.begin()+x+n+1).在一个字符串(string1)后面加上x个一样的字符(char c)。string1.append(size_t x,char c).初始化时使字符串为n个字符c:string string1(n,char c);
示例程序:
#include<string>
usingnamespace std;
intmain()
{
string s1="abc";
string s2="abcdef";
string s3="abcdefghi";
cout<<s1<<endl;
s1.append(s2,3,3);
cout<<s1<<endl;
s1.append(s3.begin()+6,s3.begin()+9);
cout<<s1<<endl;
s1.append(3,'X');
cout<<s1<<endl;
string s(3,’P’);
cout<<s<<endl;
return 0;
}
运行结果:
abc
abcdef
abcdefghi
abcdefghiXXX
PPP
7. 将字符c添加在字符串(string1)后面。string1.push_back(char c)
示例程序:
#include<iostream>
#include<string>
usingnamespace std;
intmain()
{
string s="a";
cout<<s<<endl;
s.push_back('b');
cout<<s<<endl;
char c='c';
s.push_back(c);
cout<<s<<endl;
return 0;
}
运行结果:
a
ab
abc
8. 删除字符串(string1)中的某些字符(直接修改原字符串)。删除单个字符(string1[x]):string1.erase(string1.begin()+x)(括号内必须为指向该字符的迭代器)。删除一段连续的字符(string1[x],string[x+n],共n+1个字符)string1.erase(x,n+1)(括号内为第一个被删除字符的索引、删除字符的个数) 或者 string1.erase(string1.begin()+x, string1.begin()+x+n+1)(括号内为指向字符的迭代器,不删除后一个迭代器指向的字符)
示例程序:
#include<iostream>
#include<string>
usingnamespace std;
intmain()
{
string s="abcdeafghijklmn";
cout<<s<<endl;
s.erase(s.begin()+5);
cout<<s<<endl;
s.erase(7,3);
cout<<s<<endl;
s.erase(s.begin(),s.begin()+7);
cout<<s<<endl;
return 0;
}
运行结果:
abcdeafghijklmn
abcdefghijklmn
abcdefgklmn
klmn
9. 删除字符串(string1)的最后一个字符:string1.pop_back();
示例程序:
#include<iostream>
#include<string>
usingnamespace std;
intmain()
{
string s="Hello World!";
cout<<s<<endl;
s.pop_back();
cout<<s<<endl;
return 0;
}
运行结果:
Hello World!
Hello World
10. 查找:
1) find(均返回第一个匹配目标的字符或字符串的开头字符的位置索引类型为size_t ,索引从0开始)
i. 在string1中查找string2:size_t index=string1.find(string2,thebeginindex)( thebeginindex>=0,string1[thebeginindex]是第一个被检查的字符)
ii. 在string1中查找某个未命名的字符串,size_t index=string1.find(“thecontentofthestring”,thebeginindex)(thebeginindex>=0,string1[thebeginindex]是第一个被检查的字符)
iii. 在string1中查找某个字符:char c. size_t index=string1.find(char c, thebeginindex)( thebeginindex>=0,string1[thebeginindex]是第一个被检查的字符)
iv. 在string1中查找char 型数组ch[].size_t index=string1.find(ch, thebeginindex)( thebeginindex>=0,string1[thebeginindex]是第一个被检查的字符)
示例程序:
#include<string>
using namespace std;
int main()
{
string s="aaabbbcccdddeeefffggg";
size_t index=s.find('a',0);
while(index!=string::npos)
{
cout<<index<<endl;
index=s.find('a',index+1);
}
index=s.find("aa",0);
while(index!=string::npos)
{
cout<<index<<endl;
index=s.find("aa",index+1);
}
string ss="cc";
index=s.find(ss,0);
while(index!=string::npos)
{
cout<<index<<endl;
index=s.find(ss,index+1);
}
char ch[]="cc";
index=s.find(ch,0);
while(index!=string::npos)
{
cout<<index<<endl;
index=s.find(ch,index+1);
}
return 0;
}
运行结果:
0
1
2
0
1
6
7
6
7
2) rfind(均返回最后一个匹配目标的字符或字符串的开头字符的位置索引类型为size_t ,索引从0开始)
i. 在string1中查找string2:size_t index=string1.rfind(string2,theendindex)(theendindex<=string::npos,string1[theendindex]是最后一个被检查的字符)
ii. 在string1中查找某个未命名的字符串,size_t index=string1.rfind(“thecontentofthestring”,theendindex)(theendindex<=string::npos,string1[theendindex]是最后一个被检查的字符)
iii. 在string1中查找某个字符:char c. size_t index=string1.rfind(c , theendindex)(theendindex<=string::npos,string1[theendindex]是最后一个被检查的字符)
v. 在string1中查找char 型数组ch[]. size_t index=string1.rfind(ch,theendindex)(theendindex<=string::npos,string1[theendindex]是最后一个被检查的字符)
示例程序:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s="aaabbbcccdddeeefffggg";
int n=s.size();
size_t index=s.rfind('a',n-1);
cout<<index<<endl;
index=s.rfind("aa",n-1);
cout<<index<<endl;
string ss="cc";
index=s.rfind(ss,n);
cout<<index<<endl;
char ch[]="cc";
index=s.rfind(ch,n);
cout<<index<<endl;
return 0;
}
运行结果:
2
1
7
7
3) find_first_of:返回第一个在要搜寻的字符串中出现过的任意一个字符的索引位置
示例程序:
#include <iostream>
#include <string>
#include <cstddef>
int main ()
{
std::string str ("Please, replace the vowels in this sentence byasterisks.");
std::size_t found = str.find_first_of("aeiou");
while (found!=std::string::npos)
{
str[found]='*';
found=str.find_first_of("aeiou",found+1);
}
std::cout << str << '\n';
return 0;
}
运行结果:
Pl**s*, r*pl*c* th* v*w*ls *n th*s s*nt*nc*by *st*r*sks.
4) find_last_of: 返回最后一个在要搜寻的字符串中出现过的任意一个字符的索引位置
示例程序:
#include <iostream>
#include <string>
void SplitFilename (const std::string&str)
{
std::cout << "Splitting: " << str << '\n';
unsigned found = str.find_last_of("/\\");
std::cout << " path: " << str.substr(0,found)<< '\n';
std::cout << " file: " << str.substr(found+1)<< '\n';
}
int main ()
{
std::string str1 ("/usr/bin/man");
std::string str2 ("c:\\windows\\winhelp.exe");
SplitFilename (str1);
SplitFilename (str2);
return 0;
}
运行结果:
Splitting: /usr/bin/man
path: /usr/bin
file: man
Splitting: c:\windows\winhelp.exe
path: c:\windows
file: winhelp.exe
5) find_first_not_of:Searchesthe string for thefirst character that does not match any of the characters specified in itsarguments.
示例程序:
//string::find_first_not_of
#include<iostream> // std::cout
#include<string> // std::string
#include<cstddef> // std::size_t
int main()
{
std::string str ("look fornon-alphabetic characters...");
std::size_t found =str.find_first_not_of("abcdefghijklmnopqrstuvwxyz ");
if (found!=std::string::npos)
{
std::cout << "The firstnon-alphabetic character is " << str[found];
std::cout << " at position" << found << '\n';
}
return 0;
}
运行结果:
The first non-alphabetic character is - at position 12
6) find_last_not_of:Searchesthe string for the lastcharacter that does not match any of the characters specified in its arguments.
示例程序:
//string::find_last_not_of
#include<iostream> // std::cout
#include<string> // std::string
#include<cstddef> // std::size_t
int main()
{
std::string str ("Please, erase trailingwhite-spaces \n");
std::string whitespaces ("\t\f\v\n\r");
std::size_t found =str.find_last_not_of(whitespaces);
if (found!=std::string::npos)
str.erase(found+1);
else
str.clear(); // str is all whitespace
std::cout << '[' << str <<"]\n";
return 0;
}
运行结果:
[Please, erase trailing white-spaces]
11. 按照字典序比较两个字符串string1,string2:int value=string1.compare(string2) value为int型,value==0,string==string2;value>0,string1>string2 ;value<0 string1<string2.
比较的规则:先从第一个字符开始比较,字符大小的顺序按照其对应的码值来比较,记住从小到大依次是:0 1 2 3 4 5 6 7 8 9 A B C D^^^X Y Z a b c ^^^x y z .第一个相同,比较第二个,第一个不同,直接根据第一个的比较出结果。若string2=string1.substr(0,string2.length()),即string2是string1的前一部分,那么短的字符串小,长的字符串大。
示例程序:
#include<iostream>
#include<string>
usingnamespace std;
intmain()
{
string s1="9",s2="A";
cout<<s1.compare(s2)<<endl;
s1="asd";
s2="a";
cout<<s1.compare(s2)<<endl;
s1="a";
s2="123";
cout<<s1.compare(s2)<<endl;
return 0;
}
运行结果:
-1
2
1
12. string::npos的含义:它是size_t 类型的最大值,使用它意味着:直到字符串的结尾;当作为返回值时,意味着没有匹配
13.