/// 產生由英文和數字組成的指定個數的隨機數
/// </summary>
/// <param name="number">產生隨機數的個數</param>
/// <returns>指定個數的隨機英數字符串</returns>
public static string GetRandomCode(int number)
{
string[] arrList = new string[] {"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F","G",
"H","I","J","K","L","M","N","O","P","Q","R","S","T","U","W","X","Y","Z"} ;
StringBuilder sb = new StringBuilder("") ;
Random random = new Random() ;
for( int i = 0 ; i < number ; i++ )
{
sb.Append(arrList[(int)random.Next(0,arrList.Length)]) ;
}
return sb.ToString() ;
}
/// <summary>
/// 判斷號碼是聯通,移動,電信中的哪個,在使用本方法前,請先驗證號碼的合法性
/// 規則:前三位爲130-133 聯通 ;前三位爲135-139或前四位爲1340-1348 移動; 其它的應該爲電信
/// </summary>
/// <param name="mobile">要判斷的號碼</param>
/// <returns>返回相應類型:1代表聯通;2代表移動;3代表電信</returns>
public static int GetMobileType(string mobile)
{
string[] chinaUnicom = new string[] {"130","131","132","133"} ;
string[] chinaMobile1 = new string[] {"135","136","137","138","139"} ;
string[] chinaMobile2 = new string[] {"1340","1341","1342","1343","1344","1345","1346","1347","1348"} ;
bool bolChinaUnicom = (Array.IndexOf(chinaUnicom,mobile.Substring(0,3)) >= 0) ;
bool bolChinaMobile1 = (Array.IndexOf(chinaMobile1,mobile.Substring(0,3)) >=0) ;
bool bolChinaMobile2 = (Array.IndexOf(chinaMobile2,mobile.Substring(0,4)) >=0) ;
if (bolChinaUnicom)
return 1 ;//聯通
if ( bolChinaMobile1 || bolChinaMobile2 )
return 2 ; //移動
return 3 ; //其他爲電信
}
注:有朋友建議,第二方法用正則表達式實現更好,確實不錯,下面把第二方法的新實現貼上:
/// 判斷號碼是聯通,移動,電信中的哪個,在使用本方法前,請先驗證號碼的合法性
/// 規則:前三位爲130-133 聯通 ;前三位爲135-139或前四位爲1340-1348 移動; 其它的應該爲電信
/// </summary>
/// <param name="mobile">要判斷的號碼</param>
/// <returns>返回相應類型:1代表聯通;2代表移動;3代表電信</returns>
public static int GetMobileType(string mobile)
{
if (IsChinaUnicomNumber(mobile))
return 1 ;
if (IsChinaMobileNumber(mobile))
return 2 ;
return 3 ;
}
//是否是聯通的號碼 測試通過
private static bool IsChinaUnicomNumber(string mobile)
{
string sPattern = "^(130|131|132|133)[0-9]{8}";
bool isChinaUnicom = Regex.IsMatch(mobile,sPattern) ;
return isChinaUnicom ;
}
//是否是移動的號碼 測試通過
private static bool IsChinaMobileNumber(string mobile)
{
string sPattern = "^(135|136|137|138|139|1340|1341|1342|1343|1344|1345|1346|1347|1348)[1-9]{7,8}" ;
return Regex.IsMatch(mobile,sPattern) ;
}