西安女子 發表於 2020-6-3 10:06:00

[C#.NET 拾遗补漏]01:字符串操作

<p style="box-sizing: border-box; font-size: 15px; line-height: 1.8; margin-right: 1px; margin-bottom: 20px; margin-left: 1px; letter-spacing: 0.5px; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Oxygen, Ubuntu, Cantarell, &quot;Fira Sans&quot;, &quot;Droid Sans&quot;, &quot;Helvetica Neue&quot;, sans-serif; white-space: normal">字符串操作在任意编程语言的日常编程中都随处可见,今天来汇总一下 C# 中关于字符串的一些你可能遗忘或遗漏的知识点。</p>
<h2 style="box-sizing: border-box; margin-top: 25px; margin-bottom: 20px; font-weight: bold; font-size: 20px; color: rgba(34, 34, 34, 1); padding-bottom: 3px; border-bottom: 2px solid rgba(255, 190, 0, 1); font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Oxygen, Ubuntu, Cantarell, &quot;Fira Sans&quot;, &quot;Droid Sans&quot;, &quot;Helvetica Neue&quot;, sans-serif; text-align: start; white-space: normal">逐字字符串</h2>
<p style="box-sizing: border-box; font-size: 15px; line-height: 1.8; margin-right: 1px; margin-bottom: 20px; margin-left: 1px; letter-spacing: 0.5px; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Oxygen, Ubuntu, Cantarell, &quot;Fira Sans&quot;, &quot;Droid Sans&quot;, &quot;Helvetica Neue&quot;, sans-serif; white-space: normal">在普通字符串中,反斜杠字符是转义字符。而在逐字字符串(Verbatim Strings)中,字符将被编程器按照原义进行解释。使用逐字字符串只需在字符串前面加上 @ 符号。</p>
<pre><code style="box-sizing: border-box; font-family: SFMono-Regular, Menlo, Monaco, Consolas, monospace; letter-spacing: 0; line-height: 1.45; vertical-align: middle; padding: 15px; border-radius: 5px; background: rgba(40, 44, 52, 1); overflow-wrap: unset; -webkit-box-decoration-break: clone; overflow-x: auto; border-style: none; color: rgba(171, 178, 191, 1)"><span style="box-sizing: border-box; color: rgba(92, 99, 112, 1); font-style: italic">// 逐字字符串:转义符</span><br style="box-sizing: border-box"><span style="box-sizing: border-box; color: rgba(198, 120, 221, 1)">var</span> filename = <span style="box-sizing: border-box; color: rgba(152, 195, 121, 1)">@"c:\temp\newfile.txt"</span>;<br style="box-sizing: border-box">Console.WriteLine(filenaame);<br style="box-sizing: border-box"><br style="box-sizing: border-box"><span style="box-sizing: border-box; color: rgba(92, 99, 112, 1); font-style: italic">// 逐字字符串:多行文本</span><br style="box-sizing: border-box"><span style="box-sizing: border-box; color: rgba(198, 120, 221, 1)">var</span> multiLine = <span style="box-sizing: border-box; color: rgba(152, 195, 121, 1)">@"This is a<br style="box-sizing: border-box">multiline paragraph."</span>;<br style="box-sizing: border-box">Console.WriteLine(multiLine);<br style="box-sizing: border-box"><br style="box-sizing: border-box"><span style="box-sizing: border-box; color: rgba(92, 99, 112, 1); font-style: italic">// 非逐字字符串</span><br style="box-sizing: border-box"><span style="box-sizing: border-box; color: rgba(198, 120, 221, 1)">var</span> escapedFilename = <span style="box-sizing: border-box; color: rgba(152, 195, 121, 1)">"c:\temp\newfile.txt"</span>;<br style="box-sizing: border-box">Console.WriteLine(escapedFilename);</code></pre>
<p style="box-sizing: border-box; font-size: 15px; line-height: 1.8; margin-right: 1px; margin-bottom: 20px; margin-left: 1px; letter-spacing: 0.5px; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Oxygen, Ubuntu, Cantarell, &quot;Fira Sans&quot;, &quot;Droid Sans&quot;, &quot;Helvetica Neue&quot;, sans-serif; white-space: normal">输出:</p>
<pre><code style="box-sizing: border-box; font-family: SFMono-Regular, Menlo, Monaco, Consolas, monospace; letter-spacing: 0; line-height: 1.45; vertical-align: middle; padding: 15px; border-radius: 5px; background: rgba(40, 44, 52, 1); overflow-wrap: unset; -webkit-box-decoration-break: clone; overflow-x: auto; border-style: none; color: rgba(171, 178, 191, 1)">c:\temp\newfile.txt<br style="box-sizing: border-box">This is a<br style="box-sizing: border-box">multiline paragraph.<br style="box-sizing: border-box">c:    emp<br style="box-sizing: border-box">ewfile.txt</code></pre>
<p style="box-sizing: border-box; font-size: 15px; line-height: 1.8; margin-right: 1px; margin-bottom: 20px; margin-left: 1px; letter-spacing: 0.5px; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Oxygen, Ubuntu, Cantarell, &quot;Fira Sans&quot;, &quot;Droid Sans&quot;, &quot;Helvetica Neue&quot;, sans-serif; white-space: normal">逐字字符串中唯一不被原样解释的字符是双引号。由于双引号是定义字符串的关键字符,所以在逐字字符串中要表达双引号需要用双引号进行转义。</p>
<pre><code style="box-sizing: border-box; font-family: SFMono-Regular, Menlo, Monaco, Consolas, monospace; letter-spacing: 0; line-height: 1.45; vertical-align: middle; padding: 15px; border-radius: 5px; background: rgba(40, 44, 52, 1); overflow-wrap: unset; -webkit-box-decoration-break: clone; overflow-x: auto; border-style: none; color: rgba(171, 178, 191, 1)">varstr = <span style="box-sizing: border-box; color: rgba(152, 195, 121, 1)">@"""I don't think so"", he said."</span>;<br style="box-sizing: border-box">Console.WriteLine(str);<br style="box-sizing: border-box"><span style="box-sizing: border-box; color: rgba(92, 99, 112, 1); font-style: italic">// 输出:"I don't think so", he said.</span></code></pre>
<p style="box-sizing: border-box; font-size: 15px; line-height: 1.8; margin-right: 1px; margin-bottom: 20px; margin-left: 1px; letter-spacing: 0.5px; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Oxygen, Ubuntu, Cantarell, &quot;Fira Sans&quot;, &quot;Droid Sans&quot;, &quot;Helvetica Neue&quot;, sans-serif; white-space: normal">在逐字字符串中也可以 $ 符号实现字符串内插值。</p>
<pre><code style="box-sizing: border-box; font-family: SFMono-Regular, Menlo, Monaco, Consolas, monospace; letter-spacing: 0; line-height: 1.45; vertical-align: middle; padding: 15px; border-radius: 5px; background: rgba(40, 44, 52, 1); overflow-wrap: unset; -webkit-box-decoration-break: clone; overflow-x: auto; border-style: none; color: rgba(171, 178, 191, 1)">Console.WriteLine(<span style="box-sizing: border-box; color: rgba(152, 195, 121, 1)">$@"Testing \n 1 2 <span style="box-sizing: border-box; color: rgba(224, 108, 117, 1)">{<span style="box-sizing: border-box; color: rgba(209, 154, 102, 1)">5</span> - <span style="box-sizing: border-box; color: rgba(209, 154, 102, 1)">2</span>}</span>"</span>);<br style="box-sizing: border-box"><span style="box-sizing: border-box; color: rgba(92, 99, 112, 1); font-style: italic">// 输出:Testing \n 1 2 3</span></code></pre>
<h2 style="box-sizing: border-box; margin-top: 25px; margin-bottom: 20px; font-weight: bold; font-size: 20px; color: rgba(34, 34, 34, 1); padding-bottom: 3px; border-bottom: 2px solid rgba(255, 190, 0, 1); font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Oxygen, Ubuntu, Cantarell, &quot;Fira Sans&quot;, &quot;Droid Sans&quot;, &quot;Helvetica Neue&quot;, sans-serif; text-align: start; white-space: normal">数字格式化转换</h2>
<p style="box-sizing: border-box; font-size: 15px; line-height: 1.8; margin-right: 1px; margin-bottom: 20px; margin-left: 1px; letter-spacing: 0.5px; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Oxygen, Ubuntu, Cantarell, &quot;Fira Sans&quot;, &quot;Droid Sans&quot;, &quot;Helvetica Neue&quot;, sans-serif; white-space: normal">典型的的格式化方法为:</p>
<pre><code style="box-sizing: border-box; font-family: SFMono-Regular, Menlo, Monaco, Consolas, monospace; letter-spacing: 0; line-height: 1.45; vertical-align: middle; padding: 15px; border-radius: 5px; background: rgba(40, 44, 52, 1); overflow-wrap: unset; -webkit-box-decoration-break: clone; overflow-x: auto; border-style: none; color: rgba(171, 178, 191, 1)"><span style="box-sizing: border-box; color: rgba(198, 120, 221, 1)">string</span>.Format(<span style="box-sizing: border-box; color: rgba(152, 195, 121, 1)">"{index[:format]}"</span>, number)</code></pre>
<p style="box-sizing: border-box; font-size: 15px; line-height: 1.8; margin-right: 1px; margin-bottom: 20px; margin-left: 1px; letter-spacing: 0.5px; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Oxygen, Ubuntu, Cantarell, &quot;Fira Sans&quot;, &quot;Droid Sans&quot;, &quot;Helvetica Neue&quot;, sans-serif; white-space: normal">可使用“0”和“#”占位符进行补位。“0” 表示位数不够位数就补充“0”,小数部分如果位数多了则会四舍五入;“#”表示占位,用于辅助“0”进行补位。</p>
<p style="box-sizing: border-box; font-size: 15px; line-height: 1.8; margin-right: 1px; margin-bottom: 20px; margin-left: 1px; letter-spacing: 0.5px; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Oxygen, Ubuntu, Cantarell, &quot;Fira Sans&quot;, &quot;Droid Sans&quot;, &quot;Helvetica Neue&quot;, sans-serif; white-space: normal">标准格式化用法:</p>
<pre><code style="box-sizing: border-box; font-family: SFMono-Regular, Menlo, Monaco, Consolas, monospace; letter-spacing: 0; line-height: 1.45; vertical-align: middle; padding: 15px; border-radius: 5px; background: rgba(40, 44, 52, 1); overflow-wrap: unset; -webkit-box-decoration-break: clone; overflow-x: auto; border-style: none; color: rgba(171, 178, 191, 1)"><span style="box-sizing: border-box; color: rgba(92, 99, 112, 1); font-style: italic">// “0”描述:占位符,如果可能,填充位</span><br style="box-sizing: border-box"><span style="box-sizing: border-box; color: rgba(198, 120, 221, 1)">string</span>.Format(<span style="box-sizing: border-box; color: rgba(152, 195, 121, 1)">"{0:000000}"</span>,<span style="box-sizing: border-box; color: rgba(209, 154, 102, 1)">1234</span>); <span style="box-sizing: border-box; color: rgba(92, 99, 112, 1); font-style: italic">// 结果:001234</span><br style="box-sizing: border-box"><br style="box-sizing: border-box"><span style="box-sizing: border-box; color: rgba(92, 99, 112, 1); font-style: italic">// “#”描述:占位符,如果可能,填充位</span><br style="box-sizing: border-box"><span style="box-sizing: border-box; color: rgba(198, 120, 221, 1)">string</span>.Format(<span style="box-sizing: border-box; color: rgba(152, 195, 121, 1)">"{0:######}"</span>,<span style="box-sizing: border-box; color: rgba(209, 154, 102, 1)">1234</span>);<span style="box-sizing: border-box; color: rgba(92, 99, 112, 1); font-style: italic">// 结果:1234</span><br style="box-sizing: border-box"><span style="box-sizing: border-box; color: rgba(198, 120, 221, 1)">string</span>.Format(<span style="box-sizing: border-box; color: rgba(152, 195, 121, 1)">"{0:#0####}"</span>,<span style="box-sizing: border-box; color: rgba(209, 154, 102, 1)">1234</span>);<span style="box-sizing: border-box; color: rgba(92, 99, 112, 1); font-style: italic">// 结果:01234</span><br style="box-sizing: border-box"><span style="box-sizing: border-box; color: rgba(198, 120, 221, 1)">string</span>.Format(<span style="box-sizing: border-box; color: rgba(152, 195, 121, 1)">"{0:0#0####}"</span>,<span style="box-sizing: border-box; color: rgba(209, 154, 102, 1)">1234</span>); <span style="box-sizing: border-box; color: rgba(92, 99, 112, 1); font-style: italic">// 结果:0001234</span><br style="box-sizing: border-box"><br style="box-sizing: border-box"><span style="box-sizing: border-box; color: rgba(92, 99, 112, 1); font-style: italic">// "."描述:小数点</span><br style="box-sizing: border-box"><span style="box-sizing: border-box; color: rgba(198, 120, 221, 1)">string</span>.Format(<span style="box-sizing: border-box; color: rgba(152, 195, 121, 1)">"{0:000.000}"</span>, <span style="box-sizing: border-box; color: rgba(209, 154, 102, 1)">1234</span>);       <span style="box-sizing: border-box; color: rgba(92, 99, 112, 1); font-style: italic">// 结果:1234.000</span><br style="box-sizing: border-box"><span style="box-sizing: border-box; color: rgba(198, 120, 221, 1)">string</span>.Format(<span style="box-sizing: border-box; color: rgba(152, 195, 121, 1)">"{0:000.000}"</span>, <span style="box-sizing: border-box; color: rgba(209, 154, 102, 1)">4321.12543</span>); <span style="box-sizing: border-box; color: rgba(92, 99, 112, 1); font-style: italic">// 结果:4321.125</span><br style="box-sizing: border-box"><br style="box-sizing: border-box"><span style="box-sizing: border-box; color: rgba(92, 99, 112, 1); font-style: italic">// ","描述:千分表示</span><br style="box-sizing: border-box"><span style="box-sizing: border-box; color: rgba(198, 120, 221, 1)">string</span>.Format(<span style="box-sizing: border-box; color: rgba(152, 195, 121, 1)">"{0:0,0}"</span>, <span style="box-sizing: border-box; color: rgba(209, 154, 102, 1)">1234567</span>);   <span style="box-sizing: border-box; color: rgba(92, 99, 112, 1); font-style: italic">//结果:1,234,567</span><br style="box-sizing: border-box"><br style="box-sizing: border-box"><span style="box-sizing: border-box; color: rgba(92, 99, 112, 1); font-style: italic">// "%"描述:格式化为百分数</span><br style="box-sizing: border-box"><span style="box-sizing: border-box; color: rgba(198, 120, 221, 1)">string</span>.Format(<span style="box-sizing: border-box; color: rgba(152, 195, 121, 1)">"{0:0%}"</span>,<span style="box-sizing: border-box; color: rgba(209, 154, 102, 1)">1234</span>);      <span style="box-sizing: border-box; color: rgba(92, 99, 112, 1); font-style: italic">// 结果:123400%</span><br style="box-sizing: border-box"><span style="box-sizing: border-box; color: rgba(198, 120, 221, 1)">string</span>.Format(<span style="box-sizing: border-box; color: rgba(152, 195, 121, 1)">"{0:#%}"</span>, <span style="box-sizing: border-box; color: rgba(209, 154, 102, 1)">1234.125</span>);   <span style="box-sizing: border-box; color: rgba(92, 99, 112, 1); font-style: italic">// 结果:123413%</span><br style="box-sizing: border-box"><span style="box-sizing: border-box; color: rgba(198, 120, 221, 1)">string</span>.Format(<span style="box-sizing: border-box; color: rgba(152, 195, 121, 1)">"{0:0.00%}"</span>,<span style="box-sizing: border-box; color: rgba(209, 154, 102, 1)">1234</span>);   <span style="box-sizing: border-box; color: rgba(92, 99, 112, 1); font-style: italic">// 结果:123400.00%</span><br style="box-sizing: border-box"><span style="box-sizing: border-box; color: rgba(198, 120, 221, 1)">string</span>.Format(<span style="box-sizing: border-box; color: rgba(152, 195, 121, 1)">"{0:#.00%}"</span>,<span style="box-sizing: border-box; color: rgba(209, 154, 102, 1)">1234.125</span>); <span style="box-sizing: border-box; color: rgba(92, 99, 112, 1); font-style: italic">// 结果:123412.50%</span></code></pre>
<p style="box-sizing: border-box; font-size: 15px; line-height: 1.8; margin-right: 1px; margin-bottom: 20px; margin-left: 1px; letter-spacing: 0.5px; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Oxygen, Ubuntu, Cantarell, &quot;Fira Sans&quot;, &quot;Droid Sans&quot;, &quot;Helvetica Neue&quot;, sans-serif; white-space: normal">内置快捷字母格式化用法:</p>
<pre><code style="box-sizing: border-box; font-family: SFMono-Regular, Menlo, Monaco, Consolas, monospace; letter-spacing: 0; line-height: 1.45; vertical-align: middle; padding: 15px; border-radius: 5px; background: rgba(40, 44, 52, 1); overflow-wrap: unset; -webkit-box-decoration-break: clone; overflow-x: auto; border-style: none; color: rgba(171, 178, 191, 1)"><span style="box-sizing: border-box; color: rgba(92, 99, 112, 1); font-style: italic">// E-科学计数法表示</span><br style="box-sizing: border-box">(<span style="box-sizing: border-box; color: rgba(209, 154, 102, 1)">25000</span>).ToString(<span style="box-sizing: border-box; color: rgba(152, 195, 121, 1)">"E"</span>); <span style="box-sizing: border-box; color: rgba(92, 99, 112, 1); font-style: italic">// 结果:2.500000E+004</span><br style="box-sizing: border-box"><br style="box-sizing: border-box"><span style="box-sizing: border-box; color: rgba(92, 99, 112, 1); font-style: italic">// C-货币表示,带有逗号分隔符,默认小数点后保留两位,四舍五入</span><br style="box-sizing: border-box">(<span style="box-sizing: border-box; color: rgba(209, 154, 102, 1)">2.5</span>).ToString(<span style="box-sizing: border-box; color: rgba(152, 195, 121, 1)">"C"</span>); <span style="box-sizing: border-box; color: rgba(92, 99, 112, 1); font-style: italic">// 结果:¥2.50</span><br style="box-sizing: border-box"><br style="box-sizing: border-box"><span style="box-sizing: border-box; color: rgba(92, 99, 112, 1); font-style: italic">// D-十进制数</span><br style="box-sizing: border-box">(<span style="box-sizing: border-box; color: rgba(209, 154, 102, 1)">25</span>).ToString(<span style="box-sizing: border-box; color: rgba(152, 195, 121, 1)">"D5"</span>); <span style="box-sizing: border-box; color: rgba(92, 99, 112, 1); font-style: italic">// 结果:00025</span><br style="box-sizing: border-box"><br style="box-sizing: border-box"><span style="box-sizing: border-box; color: rgba(92, 99, 112, 1); font-style: italic">// F-浮点数,保留小数位数(四舍五入)</span><br style="box-sizing: border-box">(<span style="box-sizing: border-box; color: rgba(209, 154, 102, 1)">25</span>).ToString(<span style="box-sizing: border-box; color: rgba(152, 195, 121, 1)">"F2"</span>); <span style="box-sizing: border-box; color: rgba(92, 99, 112, 1); font-style: italic">// 结果:25.00</span><br style="box-sizing: border-box"><br style="box-sizing: border-box"><span style="box-sizing: border-box; color: rgba(92, 99, 112, 1); font-style: italic">// G-常规,保留指定位数的有效数字,四舍五入</span><br style="box-sizing: border-box">(<span style="box-sizing: border-box; color: rgba(209, 154, 102, 1)">2.52</span>).ToString(<span style="box-sizing: border-box; color: rgba(152, 195, 121, 1)">"G2"</span>); <span style="box-sizing: border-box; color: rgba(92, 99, 112, 1); font-style: italic">// 结果:2.5</span><br style="box-sizing: border-box"><br style="box-sizing: border-box"><span style="box-sizing: border-box; color: rgba(92, 99, 112, 1); font-style: italic">// N-带有逗号分隔符,默认小数点后保留两位,四舍五入</span><br style="box-sizing: border-box">(<span style="box-sizing: border-box; color: rgba(209, 154, 102, 1)">2500000</span>).ToString(<span style="box-sizing: border-box; color: rgba(152, 195, 121, 1)">"N"</span>); <span style="box-sizing: border-box; color: rgba(92, 99, 112, 1); font-style: italic">// 结果:2,500,000.00</span><br style="box-sizing: border-box"><br style="box-sizing: border-box"><span style="box-sizing: border-box; color: rgba(92, 99, 112, 1); font-style: italic">// X-十六进制,非整型将产生格式异常</span><br style="box-sizing: border-box">(<span style="box-sizing: border-box; color: rgba(209, 154, 102, 1)">255</span>).ToString(<span style="box-sizing: border-box; color: rgba(152, 195, 121, 1)">"X"</span>); <span style="box-sizing: border-box; color: rgba(92, 99, 112, 1); font-style: italic">// 结果:FF</span></code></pre>
<p style="box-sizing: border-box; font-size: 15px; line-height: 1.8; margin-right: 1px; margin-bottom: 20px; margin-left: 1px; letter-spacing: 0.5px; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Oxygen, Ubuntu, Cantarell, &quot;Fira Sans&quot;, &quot;Droid Sans&quot;, &quot;Helvetica Neue&quot;, sans-serif; white-space: normal"><code style="box-sizing: border-box; font-family: SFMono-Regular, Menlo, Monaco, Consolas, monospace; letter-spacing: 0; font-size: 13px; line-height: 1; vertical-align: middle; padding: 0.1em 0.3em; border-radius: 3px; background: rgba(255, 246, 234, 1); overflow-wrap: break-word; -webkit-box-decoration-break: clone">ToString</code>&nbsp;也可以自定义补零格式化:</p>
<pre><code style="box-sizing: border-box; font-family: SFMono-Regular, Menlo, Monaco, Consolas, monospace; letter-spacing: 0; line-height: 1.45; vertical-align: middle; padding: 15px; border-radius: 5px; background: rgba(40, 44, 52, 1); overflow-wrap: unset; -webkit-box-decoration-break: clone; overflow-x: auto; border-style: none; color: rgba(171, 178, 191, 1)">(<span style="box-sizing: border-box; color: rgba(209, 154, 102, 1)">15</span>).ToString(<span style="box-sizing: border-box; color: rgba(152, 195, 121, 1)">"000"</span>);            <span style="box-sizing: border-box; color: rgba(92, 99, 112, 1); font-style: italic">// 结果:015</span><br style="box-sizing: border-box">(<span style="box-sizing: border-box; color: rgba(209, 154, 102, 1)">15</span>).ToString(<span style="box-sizing: border-box; color: rgba(152, 195, 121, 1)">"value is 0"</span>);       <span style="box-sizing: border-box; color: rgba(92, 99, 112, 1); font-style: italic">// 结果:value is 15</span><br style="box-sizing: border-box">(<span style="box-sizing: border-box; color: rgba(209, 154, 102, 1)">10.456</span>).ToString(<span style="box-sizing: border-box; color: rgba(152, 195, 121, 1)">"0.00"</span>);         <span style="box-sizing: border-box; color: rgba(92, 99, 112, 1); font-style: italic">// 结果:10.46</span><br style="box-sizing: border-box">(<span style="box-sizing: border-box; color: rgba(209, 154, 102, 1)">10.456</span>).ToString(<span style="box-sizing: border-box; color: rgba(152, 195, 121, 1)">"00"</span>);         <span style="box-sizing: border-box; color: rgba(92, 99, 112, 1); font-style: italic">// 结果:10</span><br style="box-sizing: border-box">(<span style="box-sizing: border-box; color: rgba(209, 154, 102, 1)">10.456</span>).ToString(<span style="box-sizing: border-box; color: rgba(152, 195, 121, 1)">"value is 0.0"</span>); <span style="box-sizing: border-box; color: rgba(92, 99, 112, 1); font-style: italic">// 结果:value is 10.5</span></code></pre>
<p style="box-sizing: border-box; font-size: 15px; line-height: 1.8; margin-right: 1px; margin-bottom: 20px; margin-left: 1px; letter-spacing: 0.5px; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Oxygen, Ubuntu, Cantarell, &quot;Fira Sans&quot;, &quot;Droid Sans&quot;, &quot;Helvetica Neue&quot;, sans-serif; white-space: normal">转换为二进制、八进制、十六进制输出:</p>
<pre><code style="box-sizing: border-box; font-family: SFMono-Regular, Menlo, Monaco, Consolas, monospace; letter-spacing: 0; line-height: 1.45; vertical-align: middle; padding: 15px; border-radius: 5px; background: rgba(40, 44, 52, 1); overflow-wrap: unset; -webkit-box-decoration-break: clone; overflow-x: auto; border-style: none; color: rgba(171, 178, 191, 1)"><span style="box-sizing: border-box; color: rgba(198, 120, 221, 1)">int</span> number = <span style="box-sizing: border-box; color: rgba(209, 154, 102, 1)">15</span>;<br style="box-sizing: border-box">Convert.ToString(number, <span style="box-sizing: border-box; color: rgba(209, 154, 102, 1)">2</span>);<span style="box-sizing: border-box; color: rgba(92, 99, 112, 1); font-style: italic">// 结果:1111</span><br style="box-sizing: border-box">Convert.ToString(number, <span style="box-sizing: border-box; color: rgba(209, 154, 102, 1)">8</span>);<span style="box-sizing: border-box; color: rgba(92, 99, 112, 1); font-style: italic">// 结果:17</span><br style="box-sizing: border-box">Convert.ToString(number, <span style="box-sizing: border-box; color: rgba(209, 154, 102, 1)">16</span>); <span style="box-sizing: border-box; color: rgba(92, 99, 112, 1); font-style: italic">// 结果:f</span></code></pre>
<p style="box-sizing: border-box; font-size: 15px; line-height: 1.8; margin-right: 1px; margin-bottom: 20px; margin-left: 1px; letter-spacing: 0.5px; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Oxygen, Ubuntu, Cantarell, &quot;Fira Sans&quot;, &quot;Droid Sans&quot;, &quot;Helvetica Neue&quot;, sans-serif; white-space: normal">自定义格式化器:</p>
<pre><code style="box-sizing: border-box; font-family: SFMono-Regular, Menlo, Monaco, Consolas, monospace; letter-spacing: 0; line-height: 1.45; vertical-align: middle; padding: 15px; border-radius: 5px; background: rgba(40, 44, 52, 1); overflow-wrap: unset; -webkit-box-decoration-break: clone; overflow-x: auto; border-style: none; color: rgba(171, 178, 191, 1)"><span style="box-sizing: border-box; color: rgba(198, 120, 221, 1)">public </span><span style="box-sizing: border-box; color: rgba(198, 120, 221, 1)">class </span><span style="box-sizing: border-box; color: rgba(97, 174, 238, 1)">CustomFormat</span> : <span style="box-sizing: border-box; color: rgba(97, 174, 238, 1)">IFormatProvider</span>, <span style="box-sizing: border-box; color: rgba(97, 174, 238, 1)">ICustomFormatter</span><br style="box-sizing: border-box">{<br style="box-sizing: border-box">    <span style="box-sizing: border-box"><span style="box-sizing: border-box; color: rgba(198, 120, 221, 1)">public</span> <span style="box-sizing: border-box; color: rgba(198, 120, 221, 1)">string</span> <span style="box-sizing: border-box; color: rgba(97, 174, 238, 1)">Format</span>(<span style="box-sizing: border-box"><span style="box-sizing: border-box; color: rgba(198, 120, 221, 1)">string</span> format, <span style="box-sizing: border-box; color: rgba(198, 120, 221, 1)">object</span> arg, IFormatProvider formatProvider</span>)</span><br style="box-sizing: border-box">    {<br style="box-sizing: border-box">      <span style="box-sizing: border-box; color: rgba(198, 120, 221, 1)">if</span> (!<span style="box-sizing: border-box; color: rgba(198, 120, 221, 1)">this</span>.Equals(formatProvider))<br style="box-sizing: border-box">      {<br style="box-sizing: border-box">            <span style="box-sizing: border-box; color: rgba(198, 120, 221, 1)">return </span><span style="box-sizing: border-box; color: rgba(86, 182, 194, 1)">null</span>;<br style="box-sizing: border-box">      }<br style="box-sizing: border-box">      <span style="box-sizing: border-box; color: rgba(198, 120, 221, 1)">if</span> (format == <span style="box-sizing: border-box; color: rgba(152, 195, 121, 1)">"Reverse"</span>)<br style="box-sizing: border-box">      {<br style="box-sizing: border-box">            <span style="box-sizing: border-box; color: rgba(198, 120, 221, 1)">return </span><span style="box-sizing: border-box; color: rgba(198, 120, 221, 1)">string</span>.Join(<span style="box-sizing: border-box; color: rgba(152, 195, 121, 1)">""</span>, arg.ToString().Reverse());<br style="box-sizing: border-box">      }<br style="box-sizing: border-box">      <span style="box-sizing: border-box; color: rgba(198, 120, 221, 1)">return</span> arg.ToString();<br style="box-sizing: border-box">    }<br style="box-sizing: border-box"><br style="box-sizing: border-box">    <span style="box-sizing: border-box"><span style="box-sizing: border-box; color: rgba(198, 120, 221, 1)">public</span> <span style="box-sizing: border-box; color: rgba(198, 120, 221, 1)">object</span> <span style="box-sizing: border-box; color: rgba(97, 174, 238, 1)">GetFormat</span>(<span style="box-sizing: border-box">Type formatType</span>)</span><br style="box-sizing: border-box">    {<br style="box-sizing: border-box">      <span style="box-sizing: border-box; color: rgba(198, 120, 221, 1)">return</span> formatType == <span style="box-sizing: border-box; color: rgba(198, 120, 221, 1)">typeof</span>(ICustomFormatter) ? <span style="box-sizing: border-box; color: rgba(198, 120, 221, 1)">this</span> : <span style="box-sizing: border-box; color: rgba(86, 182, 194, 1)">null</span>;<br style="box-sizing: border-box">    }<br style="box-sizing: border-box">}</code></pre>
<p style="box-sizing: border-box; font-size: 15px; line-height: 1.8; margin-right: 1px; margin-bottom: 20px; margin-left: 1px; letter-spacing: 0.5px; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Oxygen, Ubuntu, Cantarell, &quot;Fira Sans&quot;, &quot;Droid Sans&quot;, &quot;Helvetica Neue&quot;, sans-serif; white-space: normal">使用自定义格式化器:</p>
<pre><code style="box-sizing: border-box; font-family: SFMono-Regular, Menlo, Monaco, Consolas, monospace; letter-spacing: 0; line-height: 1.45; vertical-align: middle; padding: 15px; border-radius: 5px; background: rgba(40, 44, 52, 1); overflow-wrap: unset; -webkit-box-decoration-break: clone; overflow-x: auto; border-style: none; color: rgba(171, 178, 191, 1)">String.Format(newCustomFormat(), <span style="box-sizing: border-box; color: rgba(152, 195, 121, 1)">"-&gt; {0:Reverse} &lt;-"</span>, <span style="box-sizing: border-box; color: rgba(152, 195, 121, 1)">"Hello World"</span>);<br style="box-sizing: border-box"><span style="box-sizing: border-box; color: rgba(92, 99, 112, 1); font-style: italic">// 输出:-&gt; dlroW olleH &lt;-</span></code></pre>
<h2 style="box-sizing: border-box; margin-top: 25px; margin-bottom: 20px; font-weight: bold; font-size: 20px; color: rgba(34, 34, 34, 1); padding-bottom: 3px; border-bottom: 2px solid rgba(255, 190, 0, 1); font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Oxygen, Ubuntu, Cantarell, &quot;Fira Sans&quot;, &quot;Droid Sans&quot;, &quot;Helvetica Neue&quot;, sans-serif; text-align: start; white-space: normal">字符串拼接</h2>
<p style="box-sizing: border-box; font-size: 15px; line-height: 1.8; margin-right: 1px; margin-bottom: 20px; margin-left: 1px; letter-spacing: 0.5px; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Oxygen, Ubuntu, Cantarell, &quot;Fira Sans&quot;, &quot;Droid Sans&quot;, &quot;Helvetica Neue&quot;, sans-serif; white-space: normal">将数组中的字符串拼接成一个字符串:</p>
<pre><code style="box-sizing: border-box; font-family: SFMono-Regular, Menlo, Monaco, Consolas, monospace; letter-spacing: 0; line-height: 1.45; vertical-align: middle; padding: 15px; border-radius: 5px; background: rgba(40, 44, 52, 1); overflow-wrap: unset; -webkit-box-decoration-break: clone; overflow-x: auto; border-style: none; color: rgba(171, 178, 191, 1)"><span style="box-sizing: border-box; color: rgba(198, 120, 221, 1)">var</span> parts = <span style="box-sizing: border-box; color: rgba(198, 120, 221, 1)">new</span>[] { <span style="box-sizing: border-box; color: rgba(152, 195, 121, 1)">"Foo"</span>, <span style="box-sizing: border-box; color: rgba(152, 195, 121, 1)">"Bar"</span>, <span style="box-sizing: border-box; color: rgba(152, 195, 121, 1)">"Fizz"</span>, <span style="box-sizing: border-box; color: rgba(152, 195, 121, 1)">"Buzz"</span>};<br style="box-sizing: border-box"><span style="box-sizing: border-box; color: rgba(198, 120, 221, 1)">var</span> joined = <span style="box-sizing: border-box; color: rgba(198, 120, 221, 1)">string</span>.Join(<span style="box-sizing: border-box; color: rgba(152, 195, 121, 1)">", "</span>, parts);<br style="box-sizing: border-box"><span style="box-sizing: border-box; color: rgba(92, 99, 112, 1); font-style: italic">// joined = "Foo, Bar, Fizz, Buzz"</span></code></pre>
<p style="box-sizing: border-box; font-size: 15px; line-height: 1.8; margin-right: 1px; margin-bottom: 20px; margin-left: 1px; letter-spacing: 0.5px; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Oxygen, Ubuntu, Cantarell, &quot;Fira Sans&quot;, &quot;Droid Sans&quot;, &quot;Helvetica Neue&quot;, sans-serif; white-space: normal">以下四种方式都可以达到相同的字符串拼接的目的:</p>
<pre><code style="box-sizing: border-box; font-family: SFMono-Regular, Menlo, Monaco, Consolas, monospace; letter-spacing: 0; line-height: 1.45; vertical-align: middle; padding: 15px; border-radius: 5px; background: rgba(40, 44, 52, 1); overflow-wrap: unset; -webkit-box-decoration-break: clone; overflow-x: auto; border-style: none; color: rgba(171, 178, 191, 1)"><span style="box-sizing: border-box; color: rgba(198, 120, 221, 1)">string</span> first = <span style="box-sizing: border-box; color: rgba(152, 195, 121, 1)">"Hello"</span>;<br style="box-sizing: border-box"><span style="box-sizing: border-box; color: rgba(198, 120, 221, 1)">string</span> second = <span style="box-sizing: border-box; color: rgba(152, 195, 121, 1)">"World"</span>;<br style="box-sizing: border-box"><span style="box-sizing: border-box; color: rgba(198, 120, 221, 1)">string</span> foo = first + <span style="box-sizing: border-box; color: rgba(152, 195, 121, 1)">" "</span> + second;<br style="box-sizing: border-box"><span style="box-sizing: border-box; color: rgba(198, 120, 221, 1)">string</span> foo = <span style="box-sizing: border-box; color: rgba(198, 120, 221, 1)">string</span>.Concat(first, <span style="box-sizing: border-box; color: rgba(152, 195, 121, 1)">" "</span>, second);<br style="box-sizing: border-box"><span style="box-sizing: border-box; color: rgba(198, 120, 221, 1)">string</span> foo = <span style="box-sizing: border-box; color: rgba(198, 120, 221, 1)">string</span>.Format(<span style="box-sizing: border-box; color: rgba(152, 195, 121, 1)">"{0} {1}"</span>, firstname, lastname);<br style="box-sizing: border-box"><span style="box-sizing: border-box; color: rgba(198, 120, 221, 1)">string</span> foo = <span style="box-sizing: border-box; color: rgba(152, 195, 121, 1)">$"<span style="box-sizing: border-box; color: rgba(224, 108, 117, 1)">{firstname}</span> <span style="box-sizing: border-box; color: rgba(224, 108, 117, 1)">{lastname}</span>"</span>;</code></pre>
<h2 style="box-sizing: border-box; margin-top: 25px; margin-bottom: 20px; font-weight: bold; font-size: 20px; color: rgba(34, 34, 34, 1); padding-bottom: 3px; border-bottom: 2px solid rgba(255, 190, 0, 1); font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Oxygen, Ubuntu, Cantarell, &quot;Fira Sans&quot;, &quot;Droid Sans&quot;, &quot;Helvetica Neue&quot;, sans-serif; text-align: start; white-space: normal">字符串内插法</h2>
<p style="box-sizing: border-box; font-size: 15px; line-height: 1.8; margin-right: 1px; margin-bottom: 20px; margin-left: 1px; letter-spacing: 0.5px; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Oxygen, Ubuntu, Cantarell, &quot;Fira Sans&quot;, &quot;Droid Sans&quot;, &quot;Helvetica Neue&quot;, sans-serif; white-space: normal">简单用法:</p>
<pre><code style="box-sizing: border-box; font-family: SFMono-Regular, Menlo, Monaco, Consolas, monospace; letter-spacing: 0; line-height: 1.45; vertical-align: middle; padding: 15px; border-radius: 5px; background: rgba(40, 44, 52, 1); overflow-wrap: unset; -webkit-box-decoration-break: clone; overflow-x: auto; border-style: none; color: rgba(171, 178, 191, 1)"><span style="box-sizing: border-box; color: rgba(198, 120, 221, 1)">var</span> name = <span style="box-sizing: border-box; color: rgba(152, 195, 121, 1)">"World"</span>;<br style="box-sizing: border-box"><span style="box-sizing: border-box; color: rgba(198, 120, 221, 1)">var</span> str =<span style="box-sizing: border-box; color: rgba(152, 195, 121, 1)">$"Hello, <span style="box-sizing: border-box; color: rgba(224, 108, 117, 1)">{name}</span>!"</span>;<br style="box-sizing: border-box"><span style="box-sizing: border-box; color: rgba(92, 99, 112, 1); font-style: italic">// str = "Hello, World!"</span></code></pre>
<p style="box-sizing: border-box; font-size: 15px; line-height: 1.8; margin-right: 1px; margin-bottom: 20px; margin-left: 1px; letter-spacing: 0.5px; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Oxygen, Ubuntu, Cantarell, &quot;Fira Sans&quot;, &quot;Droid Sans&quot;, &quot;Helvetica Neue&quot;, sans-serif; white-space: normal">带日期格式化:</p>
<pre><code style="box-sizing: border-box; font-family: SFMono-Regular, Menlo, Monaco, Consolas, monospace; letter-spacing: 0; line-height: 1.45; vertical-align: middle; padding: 15px; border-radius: 5px; background: rgba(40, 44, 52, 1); overflow-wrap: unset; -webkit-box-decoration-break: clone; overflow-x: auto; border-style: none; color: rgba(171, 178, 191, 1)"><span style="box-sizing: border-box; color: rgba(198, 120, 221, 1)">var</span> date = DateTime.Now;<br style="box-sizing: border-box"><span style="box-sizing: border-box; color: rgba(198, 120, 221, 1)">var</span> str = <span style="box-sizing: border-box; color: rgba(152, 195, 121, 1)">$"Today is <span style="box-sizing: border-box; color: rgba(224, 108, 117, 1)">{date:yyyy-MM-dd}</span>!"</span>;</code></pre>
<p style="box-sizing: border-box; font-size: 15px; line-height: 1.8; margin-right: 1px; margin-bottom: 20px; margin-left: 1px; letter-spacing: 0.5px; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Oxygen, Ubuntu, Cantarell, &quot;Fira Sans&quot;, &quot;Droid Sans&quot;, &quot;Helvetica Neue&quot;, sans-serif; white-space: normal">补齐格式化(Padding):</p>
<pre><code style="box-sizing: border-box; font-family: SFMono-Regular, Menlo, Monaco, Consolas, monospace; letter-spacing: 0; line-height: 1.45; vertical-align: middle; padding: 15px; border-radius: 5px; background: rgba(40, 44, 52, 1); overflow-wrap: unset; -webkit-box-decoration-break: clone; overflow-x: auto; border-style: none; color: rgba(171, 178, 191, 1)"><span style="box-sizing: border-box; color: rgba(198, 120, 221, 1)">var</span> number = <span style="box-sizing: border-box; color: rgba(209, 154, 102, 1)">42</span>;<br style="box-sizing: border-box"><br style="box-sizing: border-box"><span style="box-sizing: border-box; color: rgba(92, 99, 112, 1); font-style: italic">// 向左补齐</span><br style="box-sizing: border-box"><span style="box-sizing: border-box; color: rgba(198, 120, 221, 1)">var</span> str = <span style="box-sizing: border-box; color: rgba(152, 195, 121, 1)">$"The answer to life, the universe and everything is <span style="box-sizing: border-box; color: rgba(224, 108, 117, 1)">{number, <span style="box-sizing: border-box; color: rgba(209, 154, 102, 1)">5</span>}</span>."</span>;<br style="box-sizing: border-box"><span style="box-sizing: border-box; color: rgba(92, 99, 112, 1); font-style: italic">// str = "The answer to life, the universe and everything is ___42." ('_'表示空格)</span><br style="box-sizing: border-box"><br style="box-sizing: border-box"><span style="box-sizing: border-box; color: rgba(92, 99, 112, 1); font-style: italic">// 向右补齐</span><br style="box-sizing: border-box"><span style="box-sizing: border-box; color: rgba(198, 120, 221, 1)">var</span> str = <span style="box-sizing: border-box; color: rgba(152, 195, 121, 1)">$"The answer to life, the universe and everything is $<span style="box-sizing: border-box; color: rgba(224, 108, 117, 1)">{number, <span style="box-sizing: border-box; color: rgba(209, 154, 102, 1)">-5</span>}</span>."</span>;<br style="box-sizing: border-box"><span style="box-sizing: border-box; color: rgba(92, 99, 112, 1); font-style: italic">// str = "The answer to life, the universe and everything is 42___."</span></code></pre>
<p style="box-sizing: border-box; font-size: 15px; line-height: 1.8; margin-right: 1px; margin-bottom: 20px; margin-left: 1px; letter-spacing: 0.5px; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Oxygen, Ubuntu, Cantarell, &quot;Fira Sans&quot;, &quot;Droid Sans&quot;, &quot;Helvetica Neue&quot;, sans-serif; white-space: normal">结合内置快捷字母格式化:</p>
<pre><code style="box-sizing: border-box; font-family: SFMono-Regular, Menlo, Monaco, Consolas, monospace; letter-spacing: 0; line-height: 1.45; vertical-align: middle; padding: 15px; border-radius: 5px; background: rgba(40, 44, 52, 1); overflow-wrap: unset; -webkit-box-decoration-break: clone; overflow-x: auto; border-style: none; color: rgba(171, 178, 191, 1)"><span style="box-sizing: border-box; color: rgba(198, 120, 221, 1)">var</span> amount = <span style="box-sizing: border-box; color: rgba(209, 154, 102, 1)">2.5</span>;<br style="box-sizing: border-box"><span style="box-sizing: border-box; color: rgba(198, 120, 221, 1)">var</span> str = <span style="box-sizing: border-box; color: rgba(152, 195, 121, 1)">$"It costs <span style="box-sizing: border-box; color: rgba(224, 108, 117, 1)">{amount:C}</span>"</span>;<br style="box-sizing: border-box"><span style="box-sizing: border-box; color: rgba(92, 99, 112, 1); font-style: italic">// str = "¥2.50"</span><br style="box-sizing: border-box"><br style="box-sizing: border-box"><span style="box-sizing: border-box; color: rgba(198, 120, 221, 1)">var</span> number = <span style="box-sizing: border-box; color: rgba(209, 154, 102, 1)">42</span>;<br style="box-sizing: border-box"><span style="box-sizing: border-box; color: rgba(198, 120, 221, 1)">var</span> str = <span style="box-sizing: border-box; color: rgba(152, 195, 121, 1)">$"The answer to life, the universe and everything is <span style="box-sizing: border-box; color: rgba(224, 108, 117, 1)">{number, <span style="box-sizing: border-box; color: rgba(209, 154, 102, 1)">5</span>:f1}</span>."</span>;<br style="box-sizing: border-box"><span style="box-sizing: border-box; color: rgba(92, 99, 112, 1); font-style: italic">// str = "The answer to life, the universe and everything is ___42.1"</span></code></pre>
<blockquote style="box-sizing: border-box; margin: 0 6px 20px; background-color: rgba(248, 248, 248, 1); padding: 15px; border-left: none; border-radius: 4px; quotes: none; color: rgba(85, 85, 85, 1); font-size: 14px; line-height: 1.7; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Oxygen, Ubuntu, Cantarell, &quot;Fira Sans&quot;, &quot;Droid Sans&quot;, &quot;Helvetica Neue&quot;, sans-serif; text-align: start; white-space: normal">
<p style="box-sizing: border-box; line-height: 1.7; letter-spacing: 0.5px; text-align: justify">参考:</p>
<p style="box-sizing: border-box; line-height: 1.7; letter-spacing: 0.5px; text-align: justify">1.《C# 7.0 in a Nutshell》</p>
<p style="box-sizing: border-box; line-height: 1.7; letter-spacing: 0.5px; text-align: justify">2.&nbsp;https://bit.ly/2U1eIK9</p>
</blockquote>

</div>
<div id="MySignature" role="contentinfo">
   
<div style="border: #e0e0e0 1px dashed; padding: 15px 20px; padding-left: 20px; background: #e5f1f4; position: relative">

<p style="margin-bottom: 5px">作者:精致码农-王亮</p>
<p style="margin-bottom: 5px; font-weight:bold;">
    出处:https://liamw.cn
</p>
<p style="margin-bottom: 5px">
    联系:liam.wang@live.com
</p>
<div>
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。如有问题或建议,请多多赐教,非常感谢。
</div>
</div><br><br>
来源:https://www.cnblogs.com/willick/p/13035697.html
頁: [1]
查看完整版本: [C#.NET 拾遗补漏]01:字符串操作