如何將電話號碼的格式從(###)### - ####改爲##########?有沒有最好的方法來做到這一點?我可以使用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. 使用substr格式化電話號碼
- 3. 格式化字符串/電話號碼
- 4. 在Django格式化電話號碼
- 5. 在MySQL中格式化電話號碼
- 6. 使用Eval格式化電話號碼
- 7. 格式化電話號碼ms access
- 8. Regex電話號碼實時格式化
- 9. 電話號碼格式化,OnBlur
- 10. 用linq格式化電話號碼
- 11. jqGrid tableToGrid格式化電話號碼列
- 12. Velocity模板電話號碼格式化
- 13. datagridview中格式化電話號碼
- 14. 格式化國際電話號碼
- 15. 如何格式化電話號碼
- 16. 使用libPhoneNumber格式化電話號碼
- 17. 在c中格式化電話號碼#
- 18. 電話號碼格式javascript
- 19. UItextField電話號碼格式
- 20. 將單元格值格式化爲電話號碼格式
- 21. Zapier格式電話號碼(代碼)
- 22. SQL:格式化電話號碼(插入空格)
- 23. 如何格式化電話號碼在網格列在分機
- 24. 如何編寫html表格來格式化電話號碼?
- 25. 根據國家/地區代碼格式化電話號碼
- 26. 簡化電話號碼格式的代碼
- 27. 以999-999-9999格式格式化非結構化電話號碼#輸入
- 28. 格式電話號碼的GridView
- 29. 格式包含電話號碼
- 30. oracle的電話號碼格式章EXP
'+'是不必要的,因爲函數將查找滿足指定的正則表達式的所有子字符串並將其替換。 – wei2912
@ wei2912你是對的,修好了。 – tnw