开发者

Checking if all numbers in a List are repeated

开发者 https://www.devze.com 2023-03-07 01:12 出处:网络
I need to validate phone number to check if they are all 1\'s or 2\'s or 3\'s etc. How do I effectively do that using LINK. The phone number is in a string.

I need to validate phone number to check if they are all 1's or 2's or 3's etc. How do I effectively do that using LINK. The phone number is in a string. So 开发者_开发问答if its "2323456789" is should return true and if its "3333333333" is should return false. I searched every where for a solution.


Something like this?

public bool Check(String number)
{
   return number.Distinct().Count() > 1;
}


Check out this article on validating phone numbers:

http://blog.stevenlevithan.com/archives/validate-phone-number

to quote from the document:

Area codes start with a number from 2–9, followed by 0–8, and then any third digit. The second group of three digits, known as the central office or exchange code, starts with a number from 2–9, followed by any two digits. The final four digits, known as the station code, have no restrictions.

So ya, maybe someone could put in 222-222-2222.


Linq? Just to check if a string consists of the same character repeated multiple times?

The following regular expression will match a string consisting solely of the same decimal digit repeated multiple times. That is to say, it will match "11" , "111" and "111", but not "1". If you want to to match any string consisting of the same decimal digits repeated 0 or more times, replace the 1-or-more quantifier character + character with the zero-or-more quantifier *.

private static Regex rxBogusPhoneNumber = new Regex( @"^(?<digit>\d)\k<digit>+$" ) ;
public static bool isBogusPhoneNumber( string phoneNumber )
{
  return rxBogusPhoneNumber.IsMatch( phoneNumber ) ;
}

Cheers!

0

精彩评论

暂无评论...
验证码 换一张
取 消