If I had a phone number like this
如果我有这样的电话号码
string phone = "6365555796";
Which I store with only numeric characters in my database (as a string), is it possible to output the number like this:
我只在我的数据库中存储数字字符(作为字符串),是否可以输出如下数字:
"636-555-5796"
Similar to how I could if I were using a number:
类似于我使用数字时的情况:
long phone = 6365555796;
string output = phone.ToString("000-000-0000");
I've tried searching and all I could find online were numeric formatting documents.
我试过搜索,我在网上找到的只是数字格式文件。
The reason I ask is because I think it would be an interesting idea to be able to store only the numeric values in the DB and allow for different formatting using a Constant string value to dictate how my phone numbers are formatted. Or am I better off using a number for this?
我问的原因是因为我认为能够仅在数据库中存储数值并允许使用常量字符串值来指定我的电话号码格式的不同格式是一个有趣的想法。或者我最好使用一个号码吗?
EDIT: The question is to format a string that contains numbers, not a number itself.
编辑:问题是格式化包含数字的字符串,而不是数字本身。
9 个解决方案
#1
7
Best I can think of without having to convert to a long/number and so it fits one line is:
我能想到的最好而不必转换为长/数字,所以它适合一行是:
string number = "1234567890";
string formattedNumber = string.Format("{0}-{1}-{2}", number.Substring(0,3), number.Substring(3,3), number.Substring(6));
#2
6
Be aware that not everyone uses the North American 3-3-4 format for phone numbers. European phone numbers can be up to 15 digits long, with significant punctuation, e.g. +44-XXXX-XXXX-XXXX is different from 44+XXXX-XXXX-XXXX. You are also not considering PBXs and extensions, which can require over 30 digits.
请注意,并非每个人都使用北美3-3-4格式的电话号码。欧洲电话号码最长可达15位,并带有明显的标点符号,例如+ 44-XXXX-XXXX-XXXX与44 + XXXX-XXXX-XXXX不同。您也没有考虑可能需要超过30位的PBX和扩展。
Military and radio phones can have alphabetic characters, no this is not the "2" = "ABC" you see on touch-tone phones.
军用和无线电话可以有字母字符,这不是你在按键式电话上看到的“2”=“ABC”。
#3
6
The simple version:
简单版本:
string phone = "6365555796";
Convert.ToInt64(phone).ToString("000-000-0000");
To wrap some validation around that and put it in a nice method:
要围绕它进行一些验证并将其放在一个很好的方法中:
string FormatPhone(string phone)
{
/*assume the phone string came from the database, so we already know it's
only digits. If this changes in the future a simple RegEx can validate
(or correct) the digits requirement. */
// still want to check the length:
if (phone.Length != 10) throw new InvalidArgumentException();
return Convert.ToInt64(phone).ToString("000-000-0000");
}
#4
4
I think this works
我觉得这很有效
string s = string.Format("{0:###-###-####}", ulong.Parse(phone));
In addtion, this http://blog.stevex.net/index.php/string-formatting-in-csharp/ is a nice post on formatting strings in .NET.
另外,这个http://blog.stevex.net/index.php/string-formatting-in-csharp/是一篇关于.NET格式化字符串的好文章。
Thanks to @swilliams for clarifications.
感谢@swilliams的澄清。
#5
1
Why not just do something like this?
为什么不做这样的事情呢?
string phoneText = "6365555796";
long phoneNum = long.Parse(phoneText);
string output = phoneNum.ToString("000-000-0000");
#6
1
I loves me some extension method action:
我爱我一些扩展方法动作:
/// <summary>
/// Formats a string of nine digits into a U.S. phone number.
/// </summary>
/// <param name="value">
/// The string to format as a phone number.
/// </param>
/// <returns>
/// The numeric string as a U.S. phone number.
/// </returns>
public static string
ToUSPhone (this string value)
{
if (value == null)
{
return null;
}
long dummy;
if ((value.Length != 10) ||
!long.TryParse (
value,
NumberStyles.None,
CultureInfo.CurrentCulture,
out dummy))
{
return value;
}
return string.Format (
CultureInfo.CurrentCulture,
"{0}-{1}-{2}",
value.Substring (0, 3),
value.Substring (3, 3),
value.Substring (6));
}
#7
0
You could potentially do a string insert at a given character point. Kinda like: phone = phone.Insert(3, "-"); phone = phone.Insert(7, "-");
您可以在给定的字符点处执行字符串插入。有点像:phone = phone.Insert(3,“ - ”); phone = phone.Insert(7,“ - ”);
#8
0
Sure:
Regex.Replace(phone, @"^(\d{3})(\d{3})(\d{4})$", @"$1-$2-$3")
Of course, now you have 2 problems.
当然,现在你有2个问题。
#9
-1
To control string format of a number for display in an ASP.NET Gridview or another control, you can wrap your item in a helper class for display purposes:
若要控制要在ASP.NET Gridview或其他控件中显示的数字的字符串格式,可以将项目包装在帮助程序类中以用于显示目的:
public class PhoneDisplay
{
long phoneNum;
public PhoneDisplay(long number)
{
phoneNum = number;
}
public override string ToString()
{
return string.Format("{0:###-###-####}", phoneNum);
}
}
Then, define data or display fields as (e.g.):
然后,将数据或显示字段定义为(例如):
PhoneDisplay MyPhone = new PhoneDisplay(6365555796);
#1
7
Best I can think of without having to convert to a long/number and so it fits one line is:
我能想到的最好而不必转换为长/数字,所以它适合一行是:
string number = "1234567890";
string formattedNumber = string.Format("{0}-{1}-{2}", number.Substring(0,3), number.Substring(3,3), number.Substring(6));
#2
6
Be aware that not everyone uses the North American 3-3-4 format for phone numbers. European phone numbers can be up to 15 digits long, with significant punctuation, e.g. +44-XXXX-XXXX-XXXX is different from 44+XXXX-XXXX-XXXX. You are also not considering PBXs and extensions, which can require over 30 digits.
请注意,并非每个人都使用北美3-3-4格式的电话号码。欧洲电话号码最长可达15位,并带有明显的标点符号,例如+ 44-XXXX-XXXX-XXXX与44 + XXXX-XXXX-XXXX不同。您也没有考虑可能需要超过30位的PBX和扩展。
Military and radio phones can have alphabetic characters, no this is not the "2" = "ABC" you see on touch-tone phones.
军用和无线电话可以有字母字符,这不是你在按键式电话上看到的“2”=“ABC”。
#3
6
The simple version:
简单版本:
string phone = "6365555796";
Convert.ToInt64(phone).ToString("000-000-0000");
To wrap some validation around that and put it in a nice method:
要围绕它进行一些验证并将其放在一个很好的方法中:
string FormatPhone(string phone)
{
/*assume the phone string came from the database, so we already know it's
only digits. If this changes in the future a simple RegEx can validate
(or correct) the digits requirement. */
// still want to check the length:
if (phone.Length != 10) throw new InvalidArgumentException();
return Convert.ToInt64(phone).ToString("000-000-0000");
}
#4
4
I think this works
我觉得这很有效
string s = string.Format("{0:###-###-####}", ulong.Parse(phone));
In addtion, this http://blog.stevex.net/index.php/string-formatting-in-csharp/ is a nice post on formatting strings in .NET.
另外,这个http://blog.stevex.net/index.php/string-formatting-in-csharp/是一篇关于.NET格式化字符串的好文章。
Thanks to @swilliams for clarifications.
感谢@swilliams的澄清。
#5
1
Why not just do something like this?
为什么不做这样的事情呢?
string phoneText = "6365555796";
long phoneNum = long.Parse(phoneText);
string output = phoneNum.ToString("000-000-0000");
#6
1
I loves me some extension method action:
我爱我一些扩展方法动作:
/// <summary>
/// Formats a string of nine digits into a U.S. phone number.
/// </summary>
/// <param name="value">
/// The string to format as a phone number.
/// </param>
/// <returns>
/// The numeric string as a U.S. phone number.
/// </returns>
public static string
ToUSPhone (this string value)
{
if (value == null)
{
return null;
}
long dummy;
if ((value.Length != 10) ||
!long.TryParse (
value,
NumberStyles.None,
CultureInfo.CurrentCulture,
out dummy))
{
return value;
}
return string.Format (
CultureInfo.CurrentCulture,
"{0}-{1}-{2}",
value.Substring (0, 3),
value.Substring (3, 3),
value.Substring (6));
}
#7
0
You could potentially do a string insert at a given character point. Kinda like: phone = phone.Insert(3, "-"); phone = phone.Insert(7, "-");
您可以在给定的字符点处执行字符串插入。有点像:phone = phone.Insert(3,“ - ”); phone = phone.Insert(7,“ - ”);
#8
0
Sure:
Regex.Replace(phone, @"^(\d{3})(\d{3})(\d{4})$", @"$1-$2-$3")
Of course, now you have 2 problems.
当然,现在你有2个问题。
#9
-1
To control string format of a number for display in an ASP.NET Gridview or another control, you can wrap your item in a helper class for display purposes:
若要控制要在ASP.NET Gridview或其他控件中显示的数字的字符串格式,可以将项目包装在帮助程序类中以用于显示目的:
public class PhoneDisplay
{
long phoneNum;
public PhoneDisplay(long number)
{
phoneNum = number;
}
public override string ToString()
{
return string.Format("{0:###-###-####}", phoneNum);
}
}
Then, define data or display fields as (e.g.):
然后,将数据或显示字段定义为(例如):
PhoneDisplay MyPhone = new PhoneDisplay(6365555796);