Text.remove用于在vb.net中设置电话号码格式
我有一个用于电话号码的文本框,它将电话号码格式化为如下所示: (123)456-7891 但我希望在用户完成数据输入后,它会变回数字: 1234567891 以下是我设置数字格式的代码:Text.remove用于在vb.net中设置电话号码格式,vb.net,string,substring,string-formatting,phone-number,Vb.net,String,Substring,String Formatting,Phone Number,我有一个用于电话号码的文本框,它将电话号码格式化为如下所示: (123)456-7891 但我希望在用户完成数据输入后,它会变回数字: 1234567891 以下是我设置数字格式的代码: Dim newNumber As String If txtContactNumberNew.Text.Length = 0 Then txtContactNumberNew.Text = "N/A" ElseIf txtContactNumberNew.Text
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
Dim newNumber作为字符串
如果txtContactNumberNew.Text.Length=0,则
txtContactNumberNew.Text=“不适用”
ElseIf txtContactNumberNew.Text.Length=10,则
newNumber=“(“&txtContactNumberNew.Text.Substring(0,3)&”)”&txtContactNumberNew.Text.Substring(3,3)&“-”&txtContactNumberNew.Text.Substring(6,4)
txtContactNumberNew.Text=newNumber
如果txtContactNumberNew.Text.Length>10,则
newNumber=“(“&txtContactNumberNew.Text.Substring(0,3)&”)”&txtContactNumberNew.Text.Substring(3,3)&“-”&txtContactNumberNew.Text.Substring(6,4)&“x”&txtContactNumberNew.Text.Substring(10)
txtContactNumberNew.Text=newNumber
如果txtContactNumberNew.Text.Length<10,则
MsgBox(“电话号码不足!请重新键入电话号码”,请使用感叹词“电话号码不足”)
返回
如果结束
既然我加了括号,这会改变我的字符数吗?我只想在表单完成后删除格式设置。如果您只想删除()就不能使用replace from regex吗
System.Text.RegularExpressions.Regex.Replace(str,"[\(|\)|\-|\s]","")
考虑使用修剪方法删除不需要的字符。然后可以获得结果的字符计数
'List of characters to remove
Dim charsToTrim() As Char = { "("c, ")"c, "-"c}
Dim result As String = txtContactNumberNew.Text.Trim(charsToTrim)
取自