{"id":17,"date":"2023-10-18T15:59:18","date_gmt":"2023-10-18T15:59:18","guid":{"rendered":"http:\/\/kenneth.mobi\/?p=17"},"modified":"2023-10-18T17:12:07","modified_gmt":"2023-10-18T17:12:07","slug":"02-string-format%e7%9a%84%e7%ae%80%e5%8c%96","status":"publish","type":"post","link":"https:\/\/www.kenneth.mobi\/?p=17","title":{"rendered":"02.String.Format\u7684\u7b80\u5316"},"content":{"rendered":"<pre><code class=\"language-csharp\">class NewFormat\n{\n\/\/\u65e7\u7528\u6cd5\uff1astring.Format(\"{0},{1}\",\u53d8\u91cf1\uff0c\u53d8\u91cf2)\u5b9e\u73b0\u683c\u5f0f\u5316\u8f93\u51fa\npublicvoidOldMethod()\n{\nStudent objStudent=newNewDemo.Student();\nstrings1=string.Format(\"{0},{1}\", objStudent.Name, objStudent.Age);\nstrings2=string.Format(\"\u59d3\u540d={0},\u5e74\u9f84={1}\", objStudent.Name, objStudent.Age);\nConsole.WriteLine(\"{0},\\r\\n{1}\", s1, s2);\nConsole.WriteLine();\nstrings3=string.Format(\"{0,10},{1:d3}\", objStudent.Name, objStudent.Age);\nstrings4=string.Format(\"{0,10},{1,10:d3}\", objStudent.Name, objStudent.Age);\nConsole.WriteLine(\"{0},\\r\\n{1}\", s3, s4);\nConsole.WriteLine();\nstrings5=string.Format(\"{0:yyyy-MM-dd}\", DateTime.Now);\nConsole.WriteLine(s5);\nConsole.WriteLine();\nstringsql=\"select Name from Students where StudentId={0} and Age>{1}\";\nConsole.WriteLine(sql, objStudent.StudentId, objStudent.Age);\n}\n\/\/\u65b0\u7528\u6cd5\uff1a\u5728\u5b57\u7b26\u4e32\u524d\u9762\u6dfb\u52a0\u201c$\u201d\u524d\u7f00\uff0c\uff08\u53d8\u91cf\u53ef\u4ee5\u76f4\u63a5\u5199\u5230{}\u5185\uff0c\u5e76\u4e14\u6709\u5f88\u5f3a\u7684\u667a\u80fd\u63d0\u793a\uff09\npublic void NewMethod()\n{\nStudent objStudent=newNewDemo.Student();\nstrings1=$\"{objStudent.Name },{objStudent.Age }\";\nstrings2=$\"\u59d3\u540d={objStudent.Name },\u5e74\u9f84={objStudent.Age }\";\nConsole.WriteLine($\"{ s1} ,\\r\\n{ s2} \");\nstrings3=$\"{objStudent.Name,10},{objStudent.Age:d3}\";\nstrings4=$\"{objStudent.Name,10},{objStudent.Age,10:d3}\";\nConsole.WriteLine($\"{ s3} ,\\r\\n{ s4} \");\nstrings5=$\"{DateTime.Now:yyyy-MM-dd}\";\nConsole.WriteLine(s5);\n\/\/\u5178\u578b\u5e94\u7528\nstringsql=$\"select Name from Students where StudentId={objStudent.StudentId} and Age>{objStudent.Age }\";\nConsole.WriteLine( sql);\n}\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>class NewFormat { \/\/\u65e7\u7528\u6cd5\uff1astring.Format(&#8220;{0},{1}&#8221;,\u53d8\u91cf1\uff0c\u53d8\u91cf2&#8230; &raquo; <a class=\"read-more-link\" href=\"https:\/\/www.kenneth.mobi\/?p=17\">\u95b1\u8b80\u5168\u6587<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"aside","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-17","post","type-post","status-publish","format-aside","hentry","category-c","post_format-post-format-aside"],"_links":{"self":[{"href":"https:\/\/www.kenneth.mobi\/index.php?rest_route=\/wp\/v2\/posts\/17","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kenneth.mobi\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kenneth.mobi\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kenneth.mobi\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kenneth.mobi\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=17"}],"version-history":[{"count":2,"href":"https:\/\/www.kenneth.mobi\/index.php?rest_route=\/wp\/v2\/posts\/17\/revisions"}],"predecessor-version":[{"id":43,"href":"https:\/\/www.kenneth.mobi\/index.php?rest_route=\/wp\/v2\/posts\/17\/revisions\/43"}],"wp:attachment":[{"href":"https:\/\/www.kenneth.mobi\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=17"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kenneth.mobi\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=17"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kenneth.mobi\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=17"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}