简体   繁体   English

如何在ToString()中转义点

[英]How can I escape a dot in ToString()

I have a string phone number that I want to format: 我有一个要格式化的电话号码字符串:

string PhoneNumber { get; set; }

[..]
long fonenum;

if (Int64.TryParse(PhoneNumber, out fonenum)) {
    if (PhoneNumber.Length <= 9) {
        return fonenum.ToString("#0000.0000");
    }
    else if (PhoneNumber.Length == 10) {
        return fonenum.ToString("(00) 0000.0000");
    }
    [..]
}

but the "." 但是“。” is being confused with decimal place and outputs a floated number, that in my current culture will add the decimal place as ",": 与小数位相混淆并输出一个浮点数,在我当前的区域性中会将小数位添加为“,”:

3333-4444 -> 3333,4444 3333-4444-> 3333,4444

How I force to show the dot as phone number separator? 如何强制将点显示为电话号码分隔符?

Sure I could just add Culture "US" and get what I want, but I don't even think it's the right way. 当然,我可以添加“美国文化”并获得我想要的东西,但我什至不认为这是正确的方法。

Escape it by prepending a backslash: 通过在反斜杠前添加转义符来进行转义:

fonenum.ToString("#0000\\.0000")

The backslash above is doubled because I am using a non- verbatim string literal. 上面的反斜杠加倍,因为我使用的是非逐字字符串。

用单引号将其转义:

fonenum.ToString("#0000'.'0000")

I tested a bit more and have found another too: 我进行了更多测试,还发现了另一个:

fonenum.ToString(@"#0000\\.0000")

Related to Jon answer ("@" escapes the "\\"). 与乔恩答案有关(“ @”转义为“ \\”)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM