1. <tfoot id='FJSSp'></tfoot>
      2. <legend id='FJSSp'><style id='FJSSp'><dir id='FJSSp'><q id='FJSSp'></q></dir></style></legend>
        <i id='FJSSp'><tr id='FJSSp'><dt id='FJSSp'><q id='FJSSp'><span id='FJSSp'><b id='FJSSp'><form id='FJSSp'><ins id='FJSSp'></ins><ul id='FJSSp'></ul><sub id='FJSSp'></sub></form><legend id='FJSSp'></legend><bdo id='FJSSp'><pre id='FJSSp'><center id='FJSSp'></center></pre></bdo></b><th id='FJSSp'></th></span></q></dt></tr></i><div id='FJSSp'><tfoot id='FJSSp'></tfoot><dl id='FJSSp'><fieldset id='FJSSp'></fieldset></dl></div>

        <small id='FJSSp'></small><noframes id='FJSSp'>

          <bdo id='FJSSp'></bdo><ul id='FJSSp'></ul>

        如何在 C# 中将字符串格式化为电话号码

        How to format a string as a telephone number in C#(如何在 C# 中将字符串格式化为电话号码)
          <tfoot id='Djtja'></tfoot>

              <small id='Djtja'></small><noframes id='Djtja'>

                <tbody id='Djtja'></tbody>
            1. <i id='Djtja'><tr id='Djtja'><dt id='Djtja'><q id='Djtja'><span id='Djtja'><b id='Djtja'><form id='Djtja'><ins id='Djtja'></ins><ul id='Djtja'></ul><sub id='Djtja'></sub></form><legend id='Djtja'></legend><bdo id='Djtja'><pre id='Djtja'><center id='Djtja'></center></pre></bdo></b><th id='Djtja'></th></span></q></dt></tr></i><div id='Djtja'><tfoot id='Djtja'></tfoot><dl id='Djtja'><fieldset id='Djtja'></fieldset></dl></div>
                <bdo id='Djtja'></bdo><ul id='Djtja'></ul>
                <legend id='Djtja'><style id='Djtja'><dir id='Djtja'><q id='Djtja'></q></dir></style></legend>
                • 本文介绍了如何在 C# 中将字符串格式化为电话号码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我有一个字符串1112224444",它是一个电话号码.我想在将其存储到文件之前将其格式化为 111-222-4444.它位于数据记录中,我希望能够在没有的情况下执行此操作分配一个新变量.

                  I have a string "1112224444' it is a telephone number. I want to format as 111-222-4444 before I store it in a file. It is on a datarecord and I would prefer to be able to do this without assigning a new variable.

                  我在想:

                  String.Format("{0:###-###-####}", i["MyPhone"].ToString() );
                  

                  但这似乎并不能解决问题.

                  but that does not seem to do the trick.

                  ** 更新 **

                  好的.我选择了这个解决方案

                  Ok. I went with this solution

                  Convert.ToInt64(i["Customer Phone"]).ToString("###-###-#### ####")
                  

                  现在,当扩展名小于 4 位时,它会变得混乱.它将从右侧填写数字.所以

                  Now its gets messed up when the extension is less than 4 digits. It will fill in the numbers from the right. so

                  1112224444 333  becomes
                  
                  11-221-244 3334
                  

                  有什么想法吗?

                  推荐答案

                  请注意,此答案适用于数字数据类型(int、long).如果您以字符串开头,则需要先将其转换为数字.另外,请注意您需要验证初始字符串的长度是否至少为 10 个字符.

                  Please note, this answer works with numeric data types (int, long). If you are starting with a string, you'll need to convert it to a number first. Also, please take into account that you'll need to validate that the initial string is at least 10 characters in length.

                  来自 好页面 的例子:

                  String.Format("{0:(###) ###-####}", 8005551212);
                  
                      This will output "(800) 555-1212".
                  

                  虽然正则表达式可能会更好,但请记住旧的编程报价:

                  Although a regex may work even better, keep in mind the old programming quote:

                  有些人在遇到问题,想我知道,我会用常用表达."现在他们有两个问题.
                  --Jamie Zawinski,在 comp.lang.emacs 中

                  Some people, when confronted with a problem, think "I know, I’ll use regular expressions." Now they have two problems.
                  --Jamie Zawinski, in comp.lang.emacs

                  这篇关于如何在 C# 中将字符串格式化为电话号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Force JsonConvert.SerializeXmlNode to serialize node value as an Integer or a Boolean(强制 JsonConvert.SerializeXmlNode 将节点值序列化为整数或布尔值)
                  Using JSON to Serialize/Deserialize TimeSpan(使用 JSON 序列化/反序列化 TimeSpan)
                  Could not determine JSON object type for type quot;Classquot;(无法确定类型“Class的 JSON 对象类型.)
                  How to deserialize a JSONP response (preferably with JsonTextReader and not a string)?(如何反序列化 JSONP 响应(最好使用 JsonTextReader 而不是字符串)?)
                  how to de-serialize JSON data in which Timestamp it-self contains fields?(如何反序列化时间戳本身包含字段的JSON数据?)
                  JSON.Net custom contract serialization and Collections(JSON.Net 自定义合约序列化和集合)

                    <bdo id='eBuUn'></bdo><ul id='eBuUn'></ul>
                      <tbody id='eBuUn'></tbody>

                      1. <small id='eBuUn'></small><noframes id='eBuUn'>

                        <i id='eBuUn'><tr id='eBuUn'><dt id='eBuUn'><q id='eBuUn'><span id='eBuUn'><b id='eBuUn'><form id='eBuUn'><ins id='eBuUn'></ins><ul id='eBuUn'></ul><sub id='eBuUn'></sub></form><legend id='eBuUn'></legend><bdo id='eBuUn'><pre id='eBuUn'><center id='eBuUn'></center></pre></bdo></b><th id='eBuUn'></th></span></q></dt></tr></i><div id='eBuUn'><tfoot id='eBuUn'></tfoot><dl id='eBuUn'><fieldset id='eBuUn'></fieldset></dl></div>

                          1. <tfoot id='eBuUn'></tfoot>
                          2. <legend id='eBuUn'><style id='eBuUn'><dir id='eBuUn'><q id='eBuUn'></q></dir></style></legend>