Formatting 如何使用循环检查连字符是否位于电话号码的正确位置

Formatting 如何使用循环检查连字符是否位于电话号码的正确位置,formatting,Formatting,我想使用循环遍历字符串的每个字符,在适当的位置检查数字或连字符。例如,我如何检查电话号码格式,如604-000-0000??谢谢。用于循环字符串以验证000-000-0000格式电话号码的连字符位置的伪代码 for(i = 0; i < length of string; i++) { if((i is 3 or i is 7) and character at location i is a not hyphen) { print("non hyphen f

我想使用循环遍历字符串的每个字符,在适当的位置检查数字或连字符。例如,我如何检查电话号码格式,如604-000-0000??谢谢。

用于循环字符串以验证000-000-0000格式电话号码的连字符位置的伪代码

for(i = 0; i < length of string; i++)
{
    if((i is 3 or i is 7) and character at location i is a not hyphen)
    {
       print("non hyphen found at location " + i);
    }
    if((i is not 3 and i is not 7) and character at location i is not a digit)
    {
       print("non digit found at location " + i);
    }

}

然而,这几乎肯定不是验证电话号码的最佳方式。您应该使用现有的电话号码验证方法,或者只编写一个简单的正则表达式。

您使用的是什么语言以及您尝试过的是什么。。