?? [c++對象模型][4]指針與字符串 - itech's blog - 博客園.htm
字號:
style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #000000">; </SPAN><SPAN
style="COLOR: #008000">//</SPAN><SPAN
style="COLOR: #008000"> 123456789\0</SPAN><SPAN
style="COLOR: #008000"><BR></SPAN><SPAN
style="COLOR: #000000"> strcpy(</SPAN><SPAN
style="COLOR: #0000ff">string</SPAN><SPAN
style="COLOR: #000000">, str1);<BR><BR> strcat(</SPAN><SPAN
style="COLOR: #0000ff">string</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN
style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #800000">A</SPAN><SPAN
style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #000000">); </SPAN><SPAN
style="COLOR: #008000">//</SPAN><SPAN
style="COLOR: #008000">123456789A\0</SPAN><SPAN
style="COLOR: #008000"><BR></SPAN><SPAN
style="COLOR: #000000"><BR> </SPAN><SPAN
style="COLOR: #0000ff">int</SPAN><SPAN
style="COLOR: #000000"> r </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN
style="COLOR: #000000"> strcmp(</SPAN><SPAN
style="COLOR: #0000ff">string</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN
style="COLOR: #800000">"</SPAN><SPAN
style="COLOR: #800000">123456789B</SPAN><SPAN
style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #000000">); </SPAN><SPAN
style="COLOR: #008000">//</SPAN><SPAN
style="COLOR: #008000"> 123456789A\0 < 123456789B\0</SPAN><SPAN
style="COLOR: #008000"><BR></SPAN><SPAN
style="COLOR: #000000">}<BR><BR></SPAN><SPAN
style="COLOR: #0000ff">void</SPAN><SPAN
style="COLOR: #000000"> TestStrFunction2()<BR>{<BR> </SPAN><SPAN
style="COLOR: #0000ff">int</SPAN><SPAN
style="COLOR: #000000"> ch </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #800000">'</SPAN><SPAN style="COLOR: #800000">r</SPAN><SPAN
style="COLOR: #800000">'</SPAN><SPAN
style="COLOR: #000000">;<BR> </SPAN><SPAN
style="COLOR: #0000ff">char</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #0000ff">string</SPAN><SPAN
style="COLOR: #000000">[] </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN
style="COLOR: #800000">The quick # brown dog # jumps over # the lazy fox</SPAN><SPAN
style="COLOR: #800000">"</SPAN><SPAN
style="COLOR: #000000">;<BR> </SPAN><SPAN
style="COLOR: #0000ff">char</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN
style="COLOR: #000000">pdest </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN
style="COLOR: #000000"> NULL;<BR> pdest </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN
style="COLOR: #000000"> strchr( </SPAN><SPAN
style="COLOR: #0000ff">string</SPAN><SPAN
style="COLOR: #000000">, ch );<BR><BR> pdest </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN
style="COLOR: #000000"> NULL;<BR> </SPAN><SPAN
style="COLOR: #0000ff">char</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN
style="COLOR: #000000"> str </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #800000">dog</SPAN><SPAN
style="COLOR: #800000">"</SPAN><SPAN
style="COLOR: #000000">;<BR> pdest </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN
style="COLOR: #000000"> strstr(</SPAN><SPAN
style="COLOR: #0000ff">string</SPAN><SPAN
style="COLOR: #000000">,str);<BR><BR> pdest </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN
style="COLOR: #000000"> NULL;<BR> </SPAN><SPAN
style="COLOR: #0000ff">char</SPAN><SPAN
style="COLOR: #000000"> delims[] </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #800000">#</SPAN><SPAN
style="COLOR: #800000">"</SPAN><SPAN
style="COLOR: #000000">;<BR> pdest </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN
style="COLOR: #000000"> strtok( </SPAN><SPAN
style="COLOR: #0000ff">string</SPAN><SPAN
style="COLOR: #000000">, delims );<BR> </SPAN><SPAN
style="COLOR: #0000ff">while</SPAN><SPAN
style="COLOR: #000000">( pdest </SPAN><SPAN
style="COLOR: #000000">!=</SPAN><SPAN
style="COLOR: #000000"> NULL ) <BR> {<BR> pdest </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN
style="COLOR: #000000"> strtok( NULL, delims );<BR> }<BR>}</SPAN></SPAN></DIV>
<P> </P>
<P><SPAN style="COLOR: #ff0000">總結:</SPAN></P>
<P><SPAN
style="COLOR: #ff0000">1)以mem開始的函數用來bytes的操作,所以需要指定長度,但是以str用來操作以\0結尾的字符串,不需要指定長度。</SPAN></P>
<P><SPAN
style="COLOR: #ff0000">2)對于unicode,相應的字符串操作函數前綴為wcs,例如wcscpy,wcscat,wcscmp,wcschr,wcsstr,wcstok等。</SPAN></P>
<P><SPAN
style="COLOR: #ff0000">3)在vc中還提供了有安全檢測的字符串函數后綴_s,例如strcpy_s,strcat_s,strcmp_s,wcscpy_s,wcscat_s,wcscmp_s等。</SPAN></P>
<P><SPAN style="COLOR: #ff0000">4)char*如果指向常量區,不能被修改,且此char*不需要delete。例如 char*
pStr = "ABC";。</SPAN></P>
<P>三 std::string和std::wstring使用相當簡單哦!</P>
<P>四 完!</P>
<DIV id=MySignature>
<P>感謝,Thanks!<BR><BR>作者:<A href="http://itech.cnblogs.com/">iTech</A><BR>出處:<A
href="http://itech.cnblogs.com/">http://itech.cnblogs.com/</A>
<BR>轉載:本文版權歸作者iTech所有,轉載請注明出處,不得用于商業用途!</P></DIV>
<DIV id=EntryTag>Tag標簽: <A
href="http://www.cnblogs.com/itech/tag/[Cpp?ˉ1è±??¨????]/">[Cpp對象模型]</A></DIV></DIV>
<DIV class=postDesc>posted @ 2009-02-22 16:56 <A
href="http://www.cnblogs.com/itech/">iTech</A> 閱讀(95) <A
href="http://www.cnblogs.com/itech/archive/2009/02/22/1395491.html#Post">評論(0)</A>
<A
href="http://www.cnblogs.com/itech/admin/EditPosts.aspx?postid=1395491">編輯</A>
<A href="http://www.cnblogs.com/itech/AddToFavorite.aspx?id=1395491">收藏</A> <A
onclick="PutInWz();return false;"
href="http://www.cnblogs.com/itech/archive/2009/02/22/1395491.html#">網摘</A>
所屬分類: <A href="http://www.cnblogs.com/itech/category/170009.html">C++</A>
</DIV></DIV><IMG height=1
src="[C++對象模型][4]指針與字符串 - iTech's Blog - 博客園.files/1395491.jpg" width=1> <!--
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:dc="http://purl.org/dc/elements/1.1/"xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
<rdf:Description
rdf:about="http://www.cnblogs.com/itech/archive/2009/02/22/1395491.html"
dc:identifier="http://www.cnblogs.com/itech/archive/2009/02/22/1395491.html"
dc:title=""
trackback:ping="http://www.cnblogs.com/itech/services/trackbacks/1395491.aspx" />
</rdf:RDF>
--></DIV><!--end: topics 文章、評論容器-->
<DIV id=AjaxHolder_UpdatePanel1>
<STYLE>TD {
FONT-SIZE: 12px
}
.commentTextBox {
FONT-SIZE: 13px; FONT-FAMILY: Verdana
}
A.blue:visited {
COLOR: blue
}
A.blue:active {
COLOR: blue
}
A.blue:link {
COLOR: blue
}
A.blue:hover {
COLOR: blue
}
</STYLE>
<!--Beging Temp Save-->
<STYLE>.userData {
BEHAVIOR: url(#default#userdata)
}
</STYLE>
<DIV class=userData id=CommentsPersistDiv></DIV>
<SCRIPT type=text/javascript>
function pageLoad() {
Sys.WebForms.PageRequestManager.getInstance().add_initializeRequest(handleInitializeRequest);
//Sys.WebForms.PageRequestManager.getInstance().add_endRequest(handleEndRequest);
}
function handleInitializeRequest(sender, args) {
var prm = Sys.WebForms.PageRequestManager.getInstance();
var eid = args.get_postBackElement().id;
/*if (eid.indexOf("DeleteLink")>0)
{
args.get_postBackElement().innerHTML = "<font color='red'>正在刪除...</font>";
}
else */
if (eid.indexOf("btnSubmit") > 0) {
document.getElementById("AjaxHolder_PostComment_ltSubmitMsg").innerHTML = "正在提交...";
document.getElementById("AjaxHolder_PostComment_btnSubmit").disabled = true;
}
else if (eid.indexOf("refreshList") > 0) {
document.getElementById("AjaxHolder_PostComment_refreshList").innerHTML = "<font color='red'>正在刷新...</font>";
}
}
function TempSave(ElementID) {
try {
CommentsPersistDiv.setAttribute("CommentContent", document.getElementById(ElementID).value);
CommentsPersistDiv.save("CommentXMLStore");
}
catch (ex) {
}
}
function Restore(ElementID) {
CommentsPersistDiv.load("CommentXMLStore");
document.getElementById(ElementID).value = CommentsPersistDiv.getAttribute("CommentContent");
}
</SCRIPT>
<!--Ene TempSave-->
<DIV id=divRefreshComments
style="FONT-SIZE: 12px; MARGIN-BOTTOM: 5px; MARGIN-RIGHT: 10px; TEXT-ALIGN: right"><A
id=AjaxHolder_PostComment_refreshList
href="javascript:__doPostBack('AjaxHolder$PostComment$refreshList','')">刷新評論列表</A> </DIV>
<DIV class=commentform><SPAN id=AjaxHolder_PostComment_ltSubmitMsg
style="COLOR: red"></SPAN><BR><A name=Feedback></A>
<TABLE style="WIDTH: 600px" cellSpacing=1 cellPadding=1 border=0>
<TBODY>
<TR>
<TD colSpan=3>
<DIV id=AjaxHolder_PostComment_panelADUper></DIV></TD></TR>
<TR>
<TD></TD>
<TD></TD>
<TD></TD></TR>
<TR>
<TD width=55>姓名 </TD>
<TD colSpan=2><INPUT class=tb_comment_name
id=AjaxHolder_PostComment_tbName name=AjaxHolder$PostComment$tbName> <A
href="http://passport.cnblogs.com/login.aspx?ReturnUrl=http%3a%2f%2fwww.cnblogs.com%2fitech%2farchive%2f2009%2f02%2f22%2f1395491.html">[登錄]</A>
<A
href="http://passport.cnblogs.com/register.aspx?ReturnUrl=http%3a%2f%2fwww.cnblogs.com%2fitech%2farchive%2f2009%2f02%2f22%2f1395491.html%23Top">[注冊]</A><SPAN
id=AjaxHolder_PostComment_RequiredFieldValidator2
style="VISIBILITY: hidden; COLOR: red">請輸入你的姓名</SPAN> </TD></TR>
<TR>
<TD>主頁 </TD>
<TD><INPUT class=commenttb id=AjaxHolder_PostComment_tbUrl
name=AjaxHolder$PostComment$tbUrl> </TD>
<TD></TD></TR>
<TR id=AjaxHolder_PostComment_trEmail>
<TD>Email </TD>
<TD colSpan=2><INPUT class=commenttb id=AjaxHolder_PostComment_tbEmail
name=AjaxHolder$PostComment$tbEmail>(僅博主可見)<SPAN
id=AjaxHolder_PostComment_revContactMail
style="VISIBILITY: hidden; COLOR: red">郵件地址無效</SPAN> </TD></TR>
<TR>
<TD align=left colSpan=3>
<TABLE class=CommentForm id=AjaxHolder_PostComment_tbCaptchaImage
cellSpacing=0 cellPadding=0 border=0>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -