.net 检查字符串中的第一个字符-电话号码

.net 检查字符串中的第一个字符-电话号码,.net,vb.net,string,.net,Vb.net,String,你能帮我确认一下电话号码吗?我已经验证了它有11个字符,但我不知道如何设置第一个字符为0的条件 谢谢大家! 代码如下: Do tel_no(n) = InputBox("Enter the telephone number") If Len(tel_no(n)) < 11 Or Len(tel_no(n)) > 11 Then MsgBox("The telephone number should have 11 digits

你能帮我确认一下电话号码吗?我已经验证了它有11个字符,但我不知道如何设置第一个字符为0的条件

谢谢大家!

代码如下:

Do
        tel_no(n) = InputBox("Enter the telephone number")
        If Len(tel_no(n)) < 11 Or Len(tel_no(n)) > 11 Then
            MsgBox("The telephone number should have 11  digits and should start with 0")
        End If

Loop Until Len(tel_no(n)) = 11
Do
电话号码(n)=输入框(“输入电话号码”)
如果Len(电话号码(n))小于11或Len(电话号码(n))大于11,则
MsgBox(“电话号码应该有11位数字,并且应该以0开头”)
如果结束
循环直到Len(电话号码(n))=11
试试这个:

Dim conditionMet As Boolean = False

Do
    Dim phoneNumber As String = InputBox("Enter the telephone number")

    conditionMet = phoneNumber.Length = 11 And phoneNumber.StartsWith("0")

    If Not conditionMet Then
        MsgBox("The telephone number should have 11 digits and should start with 0")
    Else
        tel_no(n) = phoneNumber
    End If

Loop Until conditionMet

不过,我要提到的是,如果您向用户展示一个新的界面,您的用户会有更好的UI体验。

您可以尝试通过以下方式进行验证:

.......
tel_no(n) = InputBox("Enter the telephone number")
If (tel_no(n).Length <> 11) Or (tel_no(n)(0) <> "0") Then
    MsgBox("The telephone number should have 11  digits and should start with 0")
End If
.......
。。。。。。。
电话号码(n)=输入框(“输入电话号码”)
如果(电话号码(n).11)或(电话号码(n)(0)“0”),则
MsgBox(“电话号码应该有11位数字,并且应该以0开头”)
如果结束
.......

这将确保电话号码(n)的长度正好是11,索引0中的字符(第一个字符)等于零(
0
)。

正是我所需要的。谢谢@har07。祝你有个好的日伴。我用过上面的,因为它比较短。无论如何,这看起来也不错。为帮助干杯。