我有一个电话号码的文本框,可以将电话号码格式化为:
(123)456-7891
但是我想让它在用户完成数据输入时改回数字:
1234567891
这是我格式化数字的代码:
Dim newNumber As String
If txtContactNumberNew.Text.Length = 0 Then
txtContactNumberNew.Text = "N/A"
ElseIf txtContactNumberNew.Text.Length = 10 Then
newNumber = "(" & txtContactNumberNew.Text.Substring(0, 3) & ") " & txtContactNumberNew.Text.Substring(3, 3) & "-" & txtContactNumberNew.Text.Substring(6, 4)
txtContactNumberNew.Text = newNumber
ElseIf txtContactNumberNew.Text.Length > 10 Then
newNumber = "(" & txtContactNumberNew.Text.Substring(0, 3) & ") " & txtContactNumberNew.Text.Substring(3, 3) & "-" & txtContactNumberNew.Text.Substring(6, 4) & " x " & txtContactNumberNew.Text.Substring(10)
txtContactNumberNew.Text = newNumber
ElseIf txtContactNumberNew.Text.Length < 10 Then
MsgBox("Not enough Numbers! Please retype the Phone number", vbExclamation, "Not enough Numbers")
Return
End If
由于我添加了括号,这会改变我的字符数吗?我只想在表单完成后删除格式。
答案 0 :(得分:0)
如果你只是想删除(),你不能只使用来自正则表达式的替换
System.Text.RegularExpressions.Regex.Replace(str,"[\(|\)|\-|\s]","")
答案 1 :(得分:0)
考虑使用trim方法删除不需要的字符。然后,您可以获得结果的字符数。
'List of characters to remove
Dim charsToTrim() As Char = { "("c, ")"c, "-"c}
Dim result As String = txtContactNumberNew.Text.Trim(charsToTrim)