📄 字符串操作_hello123.htm
字号:
cout<<s<<endl;<BR>
cin.get();<BR>}<BR>2)<BR>#include <string><BR>#include
<iostream><BR>using namespace std;</P>
<P>void main()<BR>{<BR> char chs[] =
"hehe";<BR> string s(chs);<BR>
cout<<s<<endl;<BR>
cin.get();<BR>}<BR>3)<BR>#include <string><BR>#include
<iostream><BR>using namespace std;</P>
<P>void main()<BR>{<BR> char chs[] =
"hehe";<BR> string s(chs,1,3);
//指定从chs的索引1开始,最后复制3个字节<BR>
cout<<s<<endl;<BR>
cin.get();<BR>}<BR>4)<BR>#include <string><BR>#include
<iostream><BR>using namespace std;</P>
<P>void main()<BR>{<BR> string
s1("hehe");<BR> string s2(s1);
<BR> cout<<s2<<endl;<BR>
cin.get();<BR>}<BR>5)<BR>#include <string><BR>#include
<iostream><BR>using namespace std;</P>
<P>void main()<BR>{<BR> string
s1("hehe",2,3);<BR> string s2(s1);
<BR> cout<<s2<<endl;<BR>
cin.get();<BR>}<BR>6)<BR>#include <string><BR>#include
<iostream><BR>using namespace std;</P>
<P>void main()<BR>{<BR> char chs[] =
"hehe";<BR> string s(chs,3);
//将chs前3个字符作为初值构造<BR>
cout<<s<<endl;<BR>
cin.get();<BR>}<BR>7)<BR>#include <string><BR>#include
<iostream><BR>using namespace std;</P>
<P>void main()<BR>{<BR> string
s(10,'k'); //分配10个字符,初值都是'k'<BR>
cout<<s<<endl;<BR>
cin.get();<BR>}<BR>//以上是string类实例的构造手段,都很简单.</P>
<P>9)<BR>//赋新值<BR>#include <string><BR>#include
<iostream><BR>using namespace std;</P>
<P>void main()<BR>{<BR> string
s(10,'k'); //分配10个字符,初值都是'k'<BR>
cout<<s<<endl;<BR> s =
"hehehehe";<BR>
cout<<s<<endl;<BR>
s.assign("kdje");<BR>
cout<<s<<endl;<BR>
s.assign("fkdhfkdfd",5);
//重新分配指定字符串的前5的元素内容<BR>
cout<<s<<endl;
<BR> cin.get();<BR>}<BR>10)<BR>//swap方法交换<BR>#include
<string><BR>#include <iostream><BR>using namespace std;</P>
<P>void main()<BR>{<BR> string s1 =
"hehe";<BR> string s2 = "gagaga";<BR>
cout<<"s1 : "<<s1<<endl;<BR>
cout<<"s2 : "<<s2<<endl;<BR>
s1.swap(s2);<BR> cout<<"s1 :
"<<s1<<endl;<BR> cout<<"s2 :
"<<s2<<endl;<BR>
cin.get();<BR>}<BR>11)<BR>//+=,append(),push_back()在尾部添加字符<BR>#include
<string><BR>#include <iostream><BR>using namespace std;</P>
<P>void main()<BR>{<BR> string s =
"hehe";<BR> s += "gaga";<BR>
cout<<s<<endl;<BR>
s.append("嘿嘿");
//append()方法可以添加字符串<BR>
cout<<s<<endl;<BR>
s.push_back('k');
//push_back()方法只能添加一个字符...<BR>
cout<<s<<endl;<BR>
cin.get();<BR>}<BR>12)<BR>//insert()
插入字符.其实,insert运用好,与其他的插入操作是一样的.<BR>#include <string><BR>#include
<iostream><BR>using namespace std;</P>
<P>void main()<BR>{<BR> string s =
"hehe";<BR>
s.insert(0,"头部");
//在头部插入<BR> s.insert(s.size(),"尾部");
//在尾部插入<BR>
s.insert(s.size()/2,"中间");//在中间插入<BR>
cout<<s<<endl;<BR>
cin.get();<BR>}<BR>13)<BR>#include <string><BR>#include
<iostream><BR>using namespace std;</P>
<P>void main()<BR>{<BR> string s =
"abcdefg";<BR> s.erase(0,1);
//从索引0到索引1,即删除掉了'a'<BR>
cout<<s<<endl;<BR>
//其实,还可以使用replace方法来执行删除操作<BR>
s.replace(2,3,"");//即将指定范围内的字符替换成"",即变相删除了<BR>
cout<<s<<endl;<BR> cin.get();<BR>}</P>
<P>14)<BR>//clear() 删除全部字符<BR>#include <string><BR>#include
<iostream><BR>using namespace std;</P>
<P>void main()<BR>{<BR> string s =
"abcdefg";<BR>
cout<<s.length()<<endl;<BR>
s.clear();<BR>
cout<<s.length()<<endl;<BR>
//使用earse方法变相全删除<BR> s = "dkjfd";<BR>
cout<<s.length()<<endl;<BR>
s.erase(0,s.length());<BR>
cout<<s.length()<<endl;</P>
<P> cin.get();<BR>}<BR>15)<BR>//replace()
替换字符<BR>#include <string><BR>#include <iostream><BR>using
namespace std;</P>
<P>void main()<BR>{<BR> string s =
"abcdefg";<BR>
s.replace(2,3,"!!!!!");//从索引2开始3个字节的字符全替换成"!!!!!"<BR>
cout<<s<<endl;<BR>
cin.get();<BR>}<BR>16)<BR>//==,!=,<,<=,>,>=,compare()
比较字符串<BR>#include <string><BR>#include <iostream><BR>using
namespace std;</P>
<P>void main()<BR>{<BR> string s1 =
"abcdefg";<BR> string s2 = "abcdefg";
<BR> if (s1==s2)cout<<"s1 ==
s2"<<endl;<BR> else cout<<"s1 !=
s2"<<endl;<BR> <BR> if
(s1!=s2)cout<<"s1 != s2"<<endl;<BR> else
cout<<"s1 == s2"<<endl;<BR>
<BR> if (s1>s2)cout<<"s1 >
s2"<<endl;<BR> else cout<<"s1 <=
s2"<<endl;<BR> <BR> if
(s1<=s2)cout<<"s1 <= s2"<<endl;<BR>
else cout<<"s1 > s2"<<endl;</P>
<P> cin.get();<BR>}<BR>17)<BR>//size(),length()
返回字符数量<BR>#include <string><BR>#include <iostream><BR>using
namespace std;</P>
<P>void main()<BR>{<BR> string s =
"abcdefg";<BR>
cout<<s.size()<<endl;<BR>
cout<<s.length()<<endl;</P>
<P> cin.get();<BR>}<BR>18)<BR>//max_size()
返回字符的可能最大个数<BR>#include <string><BR>#include
<iostream><BR>using namespace std;</P>
<P>void main()<BR>{<BR> string s =
"abcdefg";<BR> cout<<s.max_size()<<endl;</P>
<P> cin.get();<BR>}<BR>19)<BR>//empty()
判断字符串是否为空<BR>#include <string><BR>#include <iostream><BR>using
namespace std;</P>
<P>void main()<BR>{<BR> string s ;<BR>
if (s.empty())<BR>
cout<<"s 为空."<<endl;<BR>
else<BR> cout<<"s
不为空."<<endl;</P>
<P> s = s + "abcdefg";<BR> if
(s.empty())<BR> cout<<"s
为空."<<endl;<BR>
else<BR> cout<<"s
不为空."<<endl;</P>
<P> cin.get();<BR>}<BR>20)<BR>// [ ], at()
存取单一字符<BR>#include <string><BR>#include <iostream><BR>using
namespace std;</P>
<P>void main()<BR>{<BR> string s =
"abcdefg1111";<BR> <BR>
cout<<"use []:"<<endl;<BR> for(int i=0;
i<s.length(); i++)<BR>
{<BR>
cout<<s[i]<<endl;<BR>
}<BR> cout<<endl;</P>
<P> cout<<"use
at():"<<endl;<BR> for(int i=0; i<s.length();
i++)<BR> {<BR>
cout<<s.at(i)<<endl;<BR>
}<BR> cout<<endl;<BR>
<BR> cin.get();<BR>}<BR>21)<BR>#include
<string><BR>#include <iostream><BR>using namespace std;</P>
<P>void main()<BR>{<BR> string s =
"abcdefg1111";<BR> <BR> const char *
chs1 = s.c_str();<BR> const char * chs2 = s.data();</P>
<P> cout<<"use
at():"<<endl;<BR> int i;<BR>
for(i=0; i<s.length(); i++)<BR>
{<BR> cout<<"c_str() :
"<<chs1[i]<<endl;<BR>
cout<<"data() : "<<chs2[i]<<endl;<BR>
}<BR> cout<<"c_str() :
"<<chs1<<endl;<BR> cout<<"data() :
"<<chs2<<endl;<BR>
cout<<endl;<BR> <BR>
cin.get();<BR>}<BR>22)<BR>// substr() 返回某个子字符串<BR>#include
<string><BR>#include <iostream><BR>using namespace std;</P>
<P>void main()<BR>{<BR> string s =
"abcdefg1111";<BR> <BR> string str =
s.substr(5,3);//从索引5开始3个字节<BR>
cout<<str<<endl;<BR> <BR>
cin.get();<BR>}<BR>23)<BR>// find 查找函数<BR>#include
<string><BR>#include <iostream><BR>using namespace std;</P>
<P>void main()<BR>{<BR> string s =
"abcdefg1111";<BR> string pattern =
"fg";<BR> string::size_type pos;<BR>
pos = s.find(pattern,0);
//从索引0开始,查找符合字符串"f"的头索引<BR>
cout<<pos<<endl;<BR> string str =
s.substr(pos,pattern.size());<BR>
cout<<str<<endl;<BR>
cin.get();<BR>}<BR>24)<BR>// begin() end() 提供类似STL的迭代器支持<BR>#include
<string><BR>#include <iostream><BR>using namespace std;</P>
<P>void main()<BR>{<BR> string s =
"abcdefg1111";<BR> for(string::iterator iter =
s.begin(); iter!=s.end(); iter++)<BR>
{<BR>
cout<<*iter<<endl;<BR>
}<BR> cout<<endl;</P>
<P>
cin.get();<BR>}<BR>
一个C++字符串存在三种大小:a)现有的字符数,函数是size()和length(),他们等效。
Empty()用来检查字符串是否为空。b)max_size()
这个大小是指当前C++字符串最多能包含的字符数,很可能和机器本身的限制或者字符串所在位置连续内存的大小有关系。我们一般情况下不用关心他,应该大小足够我们用的。但是不够用的话,会抛出length_error异常c)capacity()重新分配内存之前
string所能包含的最大字符数。这里另一个需要指出的是reserve()函数,这个函数为string重新分配内存。重新分配的大小由其参数决定,默认参数为0,这时候会对string进行非强制性缩减<BR></P></DIV></TD></TR></TBODY></TABLE><BR>
<DIV class=opt><A title=查看该分类中所有文章
href="http://hi.baidu.com/haolth/blog/category/c++">类别:c++</A> | 浏览(<SPAN
id=result></SPAN>) </DIV>
<DIV class=line></DIV>
<SCRIPT language=JavaScript>
allkey=allkey+"a88f74cf5a759b3df9dc6100_bce6524ea670c409b3de05be_";
</SCRIPT>
<DIV id=in_comment><A name=comment></A>
<DIV class=tit>网友评论:</DIV>
<SCRIPT>
function writecmt(type,id,cmtname,cmturl,cmttime){
var html1="";
if(type==1){
if(cmturl==""){
html1="<a name='"+id+"'>"+cmtname+"</a> - <span class='date'>"+cmttime+"</span> ";
}else{
html1="<a name='"+id+"' href='"+cmturl+"' target='_blank' title='"+cmturl+"'>"+cmtname+"</a> - <span class='date'>"+cmttime+"</span> ";
}
}else{
if(cmtname=="匿名网友"){
if(cmturl==""){
html1="<a name='"+id+"'>"+cmtname+"</a> - <span class='date'>"+cmttime+"</span> ";
}else{
html1="<a name='"+id+"' href='"+cmturl+"' target='_blank' title='"+cmturl+"'>"+cmtname+"</a> - <span class='date'>"+cmttime+"</span>";
}
}else{
if(cmturl==""){
html1="<div class='f14' style='display:inline'>网友:<a name='"+id+"'>"+cmtname+"</a> - <span class=\"date\">"+cmttime+"</span></div>";
}else{
html1="<div class='f14' style='display:inline'>网友:<a name='"+id+"' href='"+cmturl+"' target='_blank' title='"+cmturl+"'>"+cmtname+"</a> - <span class=\"date\">"+cmttime+"</span></div>";
}
}
}
document.write(html1);
}
</SCRIPT>
<DIV id=page></DIV></DIV>
<DIV id=in_send>
<FORM id=popFormSubmit name=form1 onsubmit="return checkcmtform()"
action=http://hiup.baidu.com/haolth/commit method=post><INPUT type=hidden
value=8 name=ct> <INPUT type=hidden value=1 name=cm> <INPUT type=hidden
value=bce6524ea670c409b3de05be name=spBlogID>
<SCRIPT language=JavaScript>
document.write("<input type='hidden' name='spRefURL' value='"+window.location.href+"'>");
</SCRIPT>
<DIV class=tit>发表评论:</DIV>
<TABLE cellSpacing=5 cellPadding=0 width=620 border=0>
<TBODY>
<TR>
<TD class=f14>姓 名:</TD>
<TD><INPUT id=spBlogCmtor style="WIDTH: 220px" onfocus=hidErr(1);
maxLength=49 onchange="checkname('spBlogCmtor')" name=spBlogCmtor>
<DIV id=nmerror style="DISPLAY: none">*姓名最长为50字节</DIV></TD></TR>
<TR id=1_err style="DISPLAY: none">
<TD> </TD>
<TD>
<DIV class=error id=1_err_con></DIV></TD></TR>
<TR>
<TD class=f14>网址或邮箱:</TD>
<TD><INPUT id=spBlogCmtURL style="WIDTH: 360px" onfocus=hidErr(2);
maxLength=128 onchange="checkeandu('spBlogCmtURL')" name=spBlogCmtURL>
(选填)</TD>
<SCRIPT>
G("spBlogCmtor").value="";
G("spBlogCmtURL").value="";
</SCRIPT>
</TR>
<TR id=2_err style="DISPLAY: none">
<TD> </TD>
<TD>
<DIV class=error id=2_err_con></DIV></TD></TR>
<TR>
<TD class=f14 vAlign=top>内 容:</TD>
<TD><TEXTAREA id=spBlogCmtText style="WIDTH: 520px; HEIGHT: 155px" onfocus=showverkey();hidErr(3); name=spBlogCmtText></TEXTAREA>
<SCRIPT>
G("spBlogCmtText").value="";
</SCRIPT>
</TD></TR>
<TR id=3_err style="DISPLAY: none">
<TD> </TD>
<TD>
<DIV class=error id=3_err_con></DIV></TD></TR>
<TR id=vercode>
<TD class=f14 vAlign=top>验证码:</TD>
<TD vAlign=top><INPUT type=hidden
value=F14F931B615949FDB48F1621426D09085A125D585C758C98135A76E2F47A316DCD7393B265C597941C8FA15C3732F5E6149F10928F233B86850FEE489AAF1A92
name=spVcode> <INPUT maxLength=4 size=6 name=spVerifyKey
autocomplete="off">请输入下图中的四位验证码,字母不区分大小写。<BR>
<SCRIPT language=JavaScript>
var imgsrc="http://post.baidu.com/cgi-bin/genimg?F14F931B615949FDB48F1621426D09085A125D585C758C98135A76E2F47A316DCD7393B265C597941C8FA15C3732F5E6149F10928F233B86850FEE489AAF1A92";
document.write("<img id='verifypic' src='"+imgsrc+"' width='120' height='40'>");
function newverifypic(){
document.getElementById("verifypic").src = imgsrc +"&t="+ Math.random();
}
</SCRIPT>
<A title=看不清左边的字符 href="javascript:newverifypic();">看不清?</A> </TD></TR>
<TR>
<TD class=f14 vAlign=top> </TD>
<TD class=f14
vAlign=top><INPUT type=submit value=发表评论 name=btn_ok></TD></TR></TBODY></TABLE></FORM></DIV><BR></DIV>
<TABLE height=8 cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD class=modbl width=7> </TD>
<TD class=modbc> </TD>
<TD class=modbr width=7> </TD></TR></TBODY></TABLE></DIV></DIV></DIV></DIV>
<SCRIPT language=javascript>
<!--
var hstr="/haolth/brwstat?key1=1";
document.write("<script src='"+hstr+"&key2="+allkey+"'><\/script>");
//-->
</SCRIPT>
<BR>
<CENTER>
<DIV id=ft>©2007 Baidu</DIV></CENTER>
<SCRIPT>
if(document.getElementById("m_blog"))
{
var imgarray = document.getElementById("m_blog").getElementsByTagName('img');
var imgw = document.getElementById("m_blog").offsetWidth;
imgw =imgw-40;
for(var i=0; i<imgarray.length; i++){
if(imgarray[i].className=="blogimg" && imgarray[i].width>=imgw) imgarray[i].width=imgw;
}
}
</SCRIPT>
</CENTER><IMG style="DISPLAY: none" src=""> </BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -