{"id":1034,"date":"2014-04-04T20:37:50","date_gmt":"2014-04-04T12:37:50","guid":{"rendered":"http:\/\/www.vcoo.cc\/blog\/?p=1034"},"modified":"2014-04-04T20:37:50","modified_gmt":"2014-04-04T12:37:50","slug":"asp-net-csharp-string-length","status":"publish","type":"post","link":"https:\/\/vcoo.cc\/blog\/1034\/","title":{"rendered":"ASP.NET C# \u4e2d\u82f1\u6587\u5b57\u7b26\u4e32\u957f\u5ea6"},"content":{"rendered":"<p>\u6211\u4eec\u90fd\u77e5\u9053C#\u4e2d\u7684string\u81ea\u5df2\u6709\u4e00\u4e2aLength\u5c5e\u6027\uff0c\u7528\u6765\u7edf\u8ba1\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\uff0c\u5982\u679c\u5b57\u7b26\u90fd\u662f\u4e2d\u6587\u6216\u90fd\u662f\u82f1\u6587\uff0c\u90a3\u6ca1\u4ec0\u4e48\u95ee\u9898\uff0c\u4f46\u5982\u679c\u51fa\u73b0\u4e2d\u82f1\u6587\u6df7\u5408\u7684\u60c5\u51b5\uff0c\u6211\u4eec\u7684\u7edf\u8ba1\u7ed3\u679c\u5c31\u4e0d\u592a\u51c6\u786e\u4e86\uff0c\u56e0\u4e3a\u5728\u8ba1\u7b97\u673a\u4e2d\uff0c\u4e2d\u6587\u5360\u4e24\u4e2a\u5b57\u8282\u7684\u7a7a\u95f4\uff0c\u82f1\u6587\u5360\u4e00\u4e2a\u5b57\u8282\u7684\u7a7a\u95f4\uff0c\u800cstring\u7684Length\u4e0d\u5177\u5907\u533a\u5206\u4e2d\u82f1\u6587\u7684\u529f\u80fd\uff0c\u6240\u4ee5\u5b83\u8ba1\u7b97\u51fa\u6765\u7684\u957f\u5ea6\u5e76\u4e0d\u51c6\u786e\uff0c\u5982\u6211\u4eec\u5728\u7f51\u9875\u4e0a\u663e\u793a\u5b57\u7b26\u65f6\uff0c\u6709\u65f6\u5019\u4e0d\u80fd\u8d85\u8fc7\u4e00\u5b9a\u7684\u957f\u5ea6\uff0c\u5982\u679c\u5b57\u7b26\u4e32\u662f\u4e2d\u82f1\u6587\u6df7\u5408\uff0c\u90a3\u8f7d\u53d6\u7684\u5b57\u7b26\u4e32\u957f\u77ed\u4e0d\u4e00\uff0c\u4e25\u91cd\u5f71\u54cd\u7f8e\u89c2\u3002<\/p>\n<p>\u5f53\u7136\uff0c\u6211\u4eec\u53ef\u4ee5\u7528\u4e00\u79cd\u975e\u5e38\u7b80\u5355\u7684\u65b9\u6cd5\u6765\u8ba1\u7b97\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\uff0c\u800c\u4e14\u662f\u4e2d\u6587\u7b97\u4e24\u4e2a\u5b57\u7b26\uff0c\u82f1\u6587\u7b97\u4e00\u4e2a\u5b57\u7b26\uff0c\u4e0b\u9762\u7ed9\u51fa\u6211\u4e00\u76f4\u5728\u7528\u7684\u4e00\u4e2a\u65b9\u6cd5\u7684\u6e90\u4ee3\u7801\uff0c\u4f9b\u5927\u5bb6\u53c2\u8003\uff1a<\/p>\n<p>[csharp]\/\/\/ &lt;summary&gt;\u3000<br \/>\n\/\/\/ \u8ba1\u7b97\u6587\u672c\u957f\u5ea6\uff0c\u533a\u5206\u4e2d\u82f1\u6587\u5b57\u7b26\uff0c\u4e2d\u6587\u7b97\u4e24\u4e2a\u957f\u5ea6\uff0c\u82f1\u6587\u7b97\u4e00\u4e2a\u957f\u5ea6<br \/>\n\/\/\/ &lt;\/summary&gt;<br \/>\n\/\/\/ &lt;param name=&quot;Text&quot;&gt;\u9700\u8ba1\u7b97\u957f\u5ea6\u7684\u5b57\u7b26\u4e32&lt;\/param&gt;<br \/>\n\/\/\/ &lt;returns&gt;int&lt;\/returns&gt;<br \/>\npublic int Text_Length(string Text)<br \/>\n{<br \/>\n    int len = 0;<br \/>\n    for (int i = 0; i &lt; Text.Length; i++)<br \/>\n    {<br \/>\n        byte[] byte_len = System.Text.Encoding.Default.GetBytes(Text.Substring(i, 1));<br \/>\n        if (byte_len.Length &gt; 1)<br \/>\n            len += 2;  \/\/\u5982\u679c\u957f\u5ea6\u5927\u4e8e1\uff0c\u662f\u4e2d\u6587\uff0c\u5360\u4e24\u4e2a\u5b57\u8282\uff0c+2<br \/>\n        else<br \/>\n            len += 1;  \/\/\u5982\u679c\u957f\u5ea6\u7b49\u4e8e1\uff0c\u662f\u82f1\u6587\uff0c\u5360\u4e00\u4e2a\u5b57\u8282\uff0c+1<br \/>\n    }<br \/>\n    return len;<br \/>\n}[\/csharp]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6211\u4eec\u90fd\u77e5\u9053C#\u4e2d\u7684string\u81ea\u5df2\u6709\u4e00\u4e2aLength\u5c5e\u6027\uff0c\u7528\u6765\u7edf\u8ba1\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\uff0c\u5982\u679c\u5b57\u7b26\u90fd\u662f\u4e2d\u6587\u6216\u90fd\u662f\u82f1\u6587\uff0c\u90a3\u6ca1\u4ec0\u4e48\u95ee\u9898\uff0c\u4f46\u5982\u679c\u51fa\u73b0\u4e2d\u82f1\u6587\u6df7\u5408\u7684\u60c5\u51b5\uff0c\u6211\u4eec\u7684\u7edf\u8ba1\u7ed3\u679c\u5c31\u4e0d\u592a\u51c6\u786e\u4e86\uff0c\u56e0\u4e3a\u5728\u8ba1\u7b97\u673a\u4e2d\uff0c\u4e2d\u6587\u5360\u4e24\u4e2a\u5b57\u8282\u7684\u7a7a\u95f4\uff0c\u82f1\u6587\u5360\u4e00\u4e2a\u5b57\u8282\u7684\u7a7a\u95f4\uff0c\u800cstring\u7684Length\u4e0d\u5177\u5907\u533a\u5206\u4e2d\u82f1\u6587\u7684\u529f\u80fd\uff0c\u6240\u4ee5\u5b83\u8ba1\u7b97\u51fa\u6765\u7684\u957f\u5ea6\u5e76\u4e0d\u51c6\u786e\uff0c\u5982\u6211\u4eec\u5728\u7f51\u9875\u4e0a\u663e\u793a\u5b57\u7b26\u65f6\uff0c\u6709\u65f6\u5019\u4e0d\u80fd\u8d85\u8fc7\u4e00\u5b9a\u7684\u957f\u5ea6\uff0c\u5982\u679c\u5b57\u7b26\u4e32\u662f\u4e2d\u82f1\u6587\u6df7\u5408\uff0c\u90a3\u8f7d\u53d6\u7684\u5b57\u7b26\u4e32\u957f\u77ed\u4e0d\u4e00\uff0c\u4e25\u91cd\u5f71\u54cd\u7f8e\u89c2\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[195,201,202],"class_list":["post-1034","post","type-post","status-publish","format-standard","hentry","category-csharp","tag-csharp-2","tag-dotnet","tag-string","cat-13-id"],"_links":{"self":[{"href":"https:\/\/vcoo.cc\/blog\/wp-json\/wp\/v2\/posts\/1034","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/vcoo.cc\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vcoo.cc\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vcoo.cc\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/vcoo.cc\/blog\/wp-json\/wp\/v2\/comments?post=1034"}],"version-history":[{"count":0,"href":"https:\/\/vcoo.cc\/blog\/wp-json\/wp\/v2\/posts\/1034\/revisions"}],"wp:attachment":[{"href":"https:\/\/vcoo.cc\/blog\/wp-json\/wp\/v2\/media?parent=1034"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vcoo.cc\/blog\/wp-json\/wp\/v2\/categories?post=1034"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vcoo.cc\/blog\/wp-json\/wp\/v2\/tags?post=1034"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}