如何将电话号码的格式从(###)### - ####改为##########?有没有最好的方法来做到这一点?我可以使用String.Substring
来获取每个数字块,然后连接它们。但是,还有其他复杂的方法吗?格式化电话号码
Q
格式化电话号码
-5
A
回答
3
一个简单的正则表达式替换如何?
string formatted = Regex.Replace(phoneNumberString, "[^0-9]", "");
这实质上只是一个白色的数字列表。看到此小提琴:http://dotnetfiddle.net/ssdWSd
输入:(123)456-7890
输出:1234567890
0
一个简单的方法是:
myString = myString.Replace("(", "");
myString = myString.Replace(")", "");
myString = myString.Replace("-", "");
用一个空字符串替换每个字符。
-1
尝试这种情况:
REGEX说明
^\((\d+)\)(\d+)-(\d+)$
Assert position at the beginning of the string «^»
Match the character “(” literally «\(»
Match the regex below and capture its match into backreference number 1 «(\d+)»
Match a single character that is a “digit” (0–9 in any Unicode script) «\d+»
Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Match the character “)” literally «\)»
Match the regex below and capture its match into backreference number 2 «(\d+)»
Match a single character that is a “digit” (0–9 in any Unicode script) «\d+»
Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Match the character “-” literally «-»
Match the regex below and capture its match into backreference number 3 «(\d+)»
Match a single character that is a “digit” (0–9 in any Unicode script) «\d+»
Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Assert position at the end of the string, or before the line break at the end of the string, if any (line feed) «$»
$1$2$3
Insert the text that was last matched by capturing group number 1 «$1»
Insert the text that was last matched by capturing group number 2 «$2»
Insert the text that was last matched by capturing group number 3 «$3»
+1
谨慎解释倒票? –
3
我会使用LINQ做到这一点:
var result = new String(phoneString.Where(x => Char.IsDigit(x)).ToArray());
虽然正则表达式也适用,这并不需要任何特殊设置。
相关问题
- 1. 格式化电话号码
- 2. Velocity模板电话号码格式化
- 3. Regex电话号码实时格式化
- 4. jqGrid tableToGrid格式化电话号码列
- 5. 格式化电话号码ms access
- 6. 使用substr格式化电话号码
- 7. 格式化字符串/电话号码
- 8. 在MySQL中格式化电话号码
- 9. 使用Eval格式化电话号码
- 10. 在Django格式化电话号码
- 11. 用linq格式化电话号码
- 12. 在c中格式化电话号码#
- 13. 如何格式化电话号码
- 14. datagridview中格式化电话号码
- 15. 电话号码格式化,OnBlur
- 16. 格式化国际电话号码
- 17. 使用libPhoneNumber格式化电话号码
- 18. 电话号码格式javascript
- 19. UItextField电话号码格式
- 20. 将单元格值格式化为电话号码格式
- 21. Zapier格式电话号码(代码)
- 22. 如何编写html表格来格式化电话号码?
- 23. 如何格式化电话号码在网格列在分机
- 24. SQL:格式化电话号码(插入空格)
- 25. 根据国家/地区代码格式化电话号码
- 26. 简化电话号码格式的代码
- 27. 以999-999-9999格式格式化非结构化电话号码#输入
- 28. 处理电话号码格式
- 29. 格式包含电话号码
- 30. 自定义电话号码格式
'+'是不必要的,因为函数将查找满足指定的正则表达式的所有子字符串并将其替换。 – wei2912
@ wei2912你是对的,修好了。 – tnw