{"id":3508,"date":"2025-04-13T13:29:17","date_gmt":"2025-04-13T05:29:17","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=3508"},"modified":"2025-04-13T13:29:17","modified_gmt":"2025-04-13T05:29:17","slug":"c-%e5%ad%97%e7%ac%a6%e4%b8%b2%ef%bc%88string%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/04\/13\/c-%e5%ad%97%e7%ac%a6%e4%b8%b2%ef%bc%88string%ef%bc%89\/","title":{"rendered":"C# \u5b57\u7b26\u4e32\uff08string\uff09"},"content":{"rendered":"\n<p>\u672c\u7ae0\u8282\u91cd\u70b9\u8bb2\u89e3 <strong>C# \u5b57\u7b26\u4e32\uff08<code>string<\/code>\uff09<\/strong>\u3002\u5185\u5bb9\u6db5\u76d6\u5b57\u7b26\u4e32\u7684\u58f0\u660e\u3001\u5e38\u7528\u64cd\u4f5c\u3001\u4e0d\u53ef\u53d8\u6027\u3001\u6027\u80fd\u4f18\u5316\uff08\u5982 <code>StringBuilder<\/code>\uff09\u3001\u591a\u8bed\u8a00\u5904\u7406\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u7b49\u65b9\u9762\uff0c\u9002\u5408\u4ece\u521d\u5b66\u5230\u8fdb\u9636\u4f7f\u7528\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e00\u3001\u4ec0\u4e48\u662f\u5b57\u7b26\u4e32\uff08<code>string<\/code>\uff09<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>C# \u4e2d\u7684 <code>string<\/code> \u662f <strong>System.String \u7c7b\u7684\u522b\u540d<\/strong>\uff0c\u7528\u4e8e\u8868\u793a Unicode \u5b57\u7b26\u7684\u5e8f\u5217\u3002<\/li>\n\n\n\n<li>\u5b57\u7b26\u4e32\u662f <strong>\u4e0d\u53ef\u53d8\uff08immutable\uff09<\/strong> \u7684\uff1a\u521b\u5efa\u540e\u4e0d\u80fd\u66f4\u6539\u5176\u5185\u5bb9\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e8c\u3001\u5b57\u7b26\u4e32\u7684\u58f0\u660e\u4e0e\u521d\u59cb\u5316<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>string s1 = \"Hello World\";              \/\/ \u76f4\u63a5\u8d4b\u503c\nstring s2 = new string('x', 5);         \/\/ \"xxxxx\"\nstring s3 = string.Empty;               \/\/ \u7a7a\u5b57\u7b26\u4e32 \"\"<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e09\u3001\u5b57\u7b26\u4e32\u5e38\u89c1\u64cd\u4f5c<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u64cd\u4f5c\u65b9\u6cd5<\/th><th>\u793a\u4f8b<\/th><th>\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td><code>Length<\/code><\/td><td><code>s.Length<\/code><\/td><td>\u83b7\u53d6\u5b57\u7b26\u6570<\/td><\/tr><tr><td><code>ToUpper()<\/code>\/<code>ToLower()<\/code><\/td><td><code>s.ToUpper()<\/code><\/td><td>\u5927\/\u5c0f\u5199\u8f6c\u6362<\/td><\/tr><tr><td><code>Contains()<\/code><\/td><td><code>s.Contains(\"lo\")<\/code><\/td><td>\u662f\u5426\u5305\u542b\u5b50\u4e32<\/td><\/tr><tr><td><code>StartsWith()<\/code><\/td><td><code>s.StartsWith(\"He\")<\/code><\/td><td>\u662f\u5426\u4ee5\u67d0\u5b57\u7b26\u4e32\u5f00\u5934<\/td><\/tr><tr><td><code>EndsWith()<\/code><\/td><td><code>s.EndsWith(\"ld\")<\/code><\/td><td>\u662f\u5426\u4ee5\u67d0\u5b57\u7b26\u4e32\u7ed3\u5c3e<\/td><\/tr><tr><td><code>IndexOf()<\/code><\/td><td><code>s.IndexOf(\"l\")<\/code><\/td><td>\u67e5\u627e\u7d22\u5f15\u4f4d\u7f6e<\/td><\/tr><tr><td><code>Substring()<\/code><\/td><td><code>s.Substring(6)<\/code><\/td><td>\u4ece\u7d22\u5f15\u5f00\u59cb\u622a\u53d6\u5b57\u7b26\u4e32<\/td><\/tr><tr><td><code>Replace()<\/code><\/td><td><code>s.Replace(\"World\", \"C#\")<\/code><\/td><td>\u66ff\u6362\u5b57\u7b26\u4e32<\/td><\/tr><tr><td><code>Split()<\/code><\/td><td><code>s.Split(' ')<\/code><\/td><td>\u6309\u5b57\u7b26\u5206\u5272<\/td><\/tr><tr><td><code>Trim()<\/code><\/td><td><code>s.Trim()<\/code><\/td><td>\u53bb\u9664\u524d\u540e\u7a7a\u767d\u7b26<\/td><\/tr><tr><td><code>string.Join()<\/code><\/td><td><code>string.Join(\",\", arr)<\/code><\/td><td>\u62fc\u63a5\u6570\u7ec4\u5143\u7d20\u4e3a\u5b57\u7b26\u4e32<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u56db\u3001\u5b57\u7b26\u4e32\u4e0d\u53ef\u53d8\u6027<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>string original = \"hello\";\nstring modified = original.Replace(\"h\", \"j\");\n\nConsole.WriteLine(original); \/\/ \"hello\"\nConsole.WriteLine(modified); \/\/ \"jello\"<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u2705 \u539f\u59cb\u5b57\u7b26\u4e32\u672a\u88ab\u6539\u53d8\uff0c\u800c\u662f\u521b\u5efa\u4e86\u65b0\u5b57\u7b26\u4e32\u3002<\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e94\u3001\u6027\u80fd\u4f18\u5316\uff1a\u4f7f\u7528 <code>StringBuilder<\/code><\/h2>\n\n\n\n<p><code>StringBuilder<\/code> \u63d0\u4f9b\u53ef\u53d8\u7684\u5b57\u7b26\u4e32\u5bf9\u8c61\uff0c\u9002\u5408\u62fc\u63a5\u5927\u91cf\u5b57\u7b26\u4e32\u573a\u666f\uff08\u5982\u5faa\u73af\uff09\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>using System.Text;\n\nStringBuilder sb = new StringBuilder();\nfor (int i = 0; i &lt; 5; i++)\n{\n    sb.Append(\"Hello \");\n}\nConsole.WriteLine(sb.ToString()); \/\/ Hello Hello Hello Hello Hello <\/code><\/pre>\n\n\n\n<p>\ud83d\udcd8 \u5b98\u65b9\u6587\u6863\uff1a<a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/api\/system.text.stringbuilder\">StringBuilder \u7c7b<\/a><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u516d\u3001\u5b57\u7b26\u4e32\u6bd4\u8f83\uff08\u533a\u5206\u5927\u5c0f\u5199\u4e0e\u5ffd\u7565\u5927\u5c0f\u5199\uff09<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>string a = \"Hello\";\nstring b = \"hello\";\n\n\/\/ \u4e25\u683c\u533a\u5206\u5927\u5c0f\u5199\nbool isEqual = a == b; \/\/ false\n\n\/\/ \u5ffd\u7565\u5927\u5c0f\u5199\nbool isEqualIgnore = string.Equals(a, b, StringComparison.OrdinalIgnoreCase); \/\/ true<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e03\u3001\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u5f0f<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u5b57\u7b26\u4e32\u63d2\u503c\uff08\u63a8\u8350\uff09<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>string name = \"Alice\";\nint age = 30;\nstring msg = $\"Name: {name}, Age: {age}\";<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. <code>string.Format()<\/code><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>string msg = string.Format(\"Name: {0}, Age: {1}\", name, age);<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3. <code>+<\/code> \u62fc\u63a5\uff08\u4e0d\u63a8\u8350\u7528\u4e8e\u5927\u91cf\u62fc\u63a5\uff09<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>string msg = \"Name: \" + name + \", Age: \" + age;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u516b\u3001\u6b63\u5219\u8868\u8fbe\u5f0f\u652f\u6301\uff08Regex\uff09<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>using System.Text.RegularExpressions;\n\nstring input = \"Email: abc@example.com\";\nstring pattern = @\"\\w+@\\w+\\.\\w+\";\n\nMatch match = Regex.Match(input, pattern);\nif (match.Success)\n{\n    Console.WriteLine(\"Email found: \" + match.Value);\n}<\/code><\/pre>\n\n\n\n<p>\ud83d\udcd8 \u6b63\u5219\u5b98\u65b9\u6587\u6863\uff1a<br><a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/standard\/base-types\/regular-expression-language-quick-reference\">.NET Regex \u6587\u6863<\/a><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e5d\u3001\u591a\u8bed\u8a00\u652f\u6301\u4e0e Unicode<\/h2>\n\n\n\n<p>C# \u7684\u5b57\u7b26\u4e32\u662f\u57fa\u4e8e Unicode \u7684\uff0c\u652f\u6301\u4e2d\u65e5\u97e9\u6587\u3001Emoji \u7b49\u5b57\u7b26\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>string emoji = \"\u4f60\u597d \ud83d\ude0a\";\nConsole.WriteLine(emoji.Length); \/\/ \u8868\u793a Unicode \u5b57\u7b26\u6570\uff08\u6ce8\u610f\u53ef\u80fd\u4e0d\u540c\u4e8e\u89c6\u89c9\u957f\u5ea6\uff09<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u5341\u3001\u5b57\u7b26\u4e32\u4e0e\u5b57\u7b26\u6570\u7ec4\u8f6c\u6362<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u5b57\u7b26\u4e32 \u2192 \u5b57\u7b26\u6570\u7ec4\nchar&#91;] chars = \"Hello\".ToCharArray();\n\n\/\/ \u5b57\u7b26\u6570\u7ec4 \u2192 \u5b57\u7b26\u4e32\nstring str = new string(chars);<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u5341\u4e00\u3001\u5b57\u7b26\u4e32\u5b9e\u7528\u5de5\u5177\u65b9\u6cd5<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u65b9\u6cd5<\/th><th>\u529f\u80fd<\/th><\/tr><\/thead><tbody><tr><td><code>string.IsNullOrEmpty(str)<\/code><\/td><td>\u5224\u65ad\u662f\u5426\u4e3a null \u6216\u7a7a\u5b57\u7b26\u4e32<\/td><\/tr><tr><td><code>string.IsNullOrWhiteSpace()<\/code><\/td><td>\u5224\u65ad\u662f\u5426\u4e3a null\u3001\u7a7a\u6216\u5168\u7a7a\u767d<\/td><\/tr><tr><td><code>string.Compare()<\/code><\/td><td>\u6bd4\u8f83\u4e24\u4e2a\u5b57\u7b26\u4e32<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u5341\u4e8c\u3001\u5b98\u65b9\u53c2\u8003\u6587\u6863<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/csharp\/programming-guide\/strings\/\">C# \u5b57\u7b26\u4e32\u57fa\u7840<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/api\/system.string?view=net-8.0\">System.String \u7c7b\u6587\u6863<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/api\/system.text.stringbuilder\">StringBuilder \u7c7b<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/standard\/base-types\/regular-expression-language-quick-reference\">\u6b63\u5219\u8868\u8fbe\u5f0f\u8bed\u8a00\u5feb\u901f\u53c2\u8003<\/a><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u5341\u4e09\u3001\u6700\u4f73\u5b9e\u8df5\u5efa\u8bae<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u5efa\u8bae<\/th><th>\u539f\u56e0<\/th><\/tr><\/thead><tbody><tr><td>\u2705 \u4f7f\u7528\u5b57\u7b26\u4e32\u63d2\u503c\u4ee3\u66ff\u62fc\u63a5<\/td><td>\u66f4\u5b89\u5168\u3001\u6e05\u6670<\/td><\/tr><tr><td>\u2705 \u591a\u6b21\u62fc\u63a5\u7528 <code>StringBuilder<\/code><\/td><td>\u907f\u514d\u5927\u91cf\u4e2d\u95f4\u5b57\u7b26\u4e32\u5bf9\u8c61<\/td><\/tr><tr><td>\u274c \u907f\u514d\u4f7f\u7528 <code>==<\/code> \u8fdb\u884c\u6587\u5316\u654f\u611f\u6bd4\u8f83<\/td><td>\u4f7f\u7528 <code>string.Equals(..., StringComparison)<\/code> \u66f4\u51c6\u786e<\/td><\/tr><tr><td>\u2705 \u4f7f\u7528 <code>IsNullOrWhiteSpace<\/code> \u68c0\u67e5\u7528\u6237\u8f93\u5165<\/td><td>\u66f4\u5065\u58ee<\/td><\/tr><tr><td>\u2705 \u6b63\u5219\u8868\u8fbe\u5f0f\u9884\u7f16\u8bd1\uff08RegexOptions.Compiled\uff09\u63d0\u9ad8\u6027\u80fd\uff08\u5982\u5728\u5faa\u73af\u4e2d\uff09<\/td><td><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\uff0c\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u7ae0\u8282\u91cd\u70b9\u8bb2\u89e3 C# \u5b57\u7b26\u4e32\uff08string\uff09\u3002\u5185\u5bb9\u6db5\u76d6\u5b57\u7b26\u4e32\u7684\u58f0\u660e\u3001\u5e38\u7528\u64cd\u4f5c\u3001\u4e0d\u53ef\u53d8\u6027\u3001\u6027\u80fd\u4f18\u5316\uff08\u5982 Stri [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3509,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[83],"tags":[],"class_list":["post-3508","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3508","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=3508"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3508\/revisions"}],"predecessor-version":[{"id":3510,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3508\/revisions\/3510"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/3509"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=3508"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=3508"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=3508"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}