今天下午寫兩個函數,還是比較通用的~~~

ExpandedBlockStart.gif        /// <summary>
InBlock.gif        
/// 產生由英文和數字組成的指定個數的隨機數
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="number">產生隨機數的個數</param>
ExpandedBlockEnd.gif        
/// <returns>指定個數的隨機英數字符串</returns>

None.gif        public static string GetRandomCode(int number)
ExpandedBlockStart.gif        
{
ExpandedSubBlockStart.gif            
string[] arrList = new string[] {"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F","G",
ExpandedSubBlockEnd.gif                                            
"H","I","J","K","L","M","N","O","P","Q","R","S","T","U","W","X","Y","Z"}
 ;
InBlock.gif            StringBuilder sb 
= new StringBuilder("") ;
InBlock.gif            Random random 
= new Random() ;
InBlock.gif
InBlock.gif            
forint i = 0 ; i < number ; i++ )
ExpandedSubBlockStart.gif            
{
InBlock.gif                sb.Append(arrList[(
int)random.Next(0,arrList.Length)]) ;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return sb.ToString() ;
ExpandedBlockEnd.gif        }

None.gif
ExpandedBlockStart.gif        
/// <summary>
InBlock.gif        
/// 判斷號碼是聯通,移動,電信中的哪個,在使用本方法前,請先驗證號碼的合法性
InBlock.gif        
/// 規則:前三位爲130-133 聯通 ;前三位爲135-139或前四位爲1340-1348 移動; 其它的應該爲電信
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="mobile">要判斷的號碼</param>
ExpandedBlockEnd.gif        
/// <returns>返回相應類型:1代表聯通;2代表移動;3代表電信</returns>

None.gif        public static int GetMobileType(string mobile) 
ExpandedBlockStart.gif        
{
ExpandedSubBlockStart.gif            
string[] chinaUnicom  = new string[] {"130","131","132","133"} ;
ExpandedSubBlockStart.gif            
string[] chinaMobile1 = new string[] {"135","136","137","138","139"} ;
ExpandedSubBlockStart.gif            
string[] chinaMobile2 = new string[] {"1340","1341","1342","1343","1344","1345","1346","1347","1348"} ;
InBlock.gif
InBlock.gif            
bool bolChinaUnicom  = (Array.IndexOf(chinaUnicom,mobile.Substring(0,3)) >= 0) ;
InBlock.gif            
bool bolChinaMobile1 = (Array.IndexOf(chinaMobile1,mobile.Substring(0,3)) >=0) ;
InBlock.gif            
bool bolChinaMobile2 = (Array.IndexOf(chinaMobile2,mobile.Substring(0,4)) >=0) ;
InBlock.gif
InBlock.gif            
if (bolChinaUnicom)
InBlock.gif                
return 1  ;//聯通
InBlock.gif

InBlock.gif            
if ( bolChinaMobile1 || bolChinaMobile2 )
InBlock.gif                
return 2 ; //移動
InBlock.gif
            
InBlock.gif            
return 3 ; //其他爲電信
ExpandedBlockEnd.gif
        }

None.gif

注:有朋友建議,第二方法用正則表達式實現更好,確實不錯,下面把第二方法的新實現貼上:
ExpandedBlockStart.gif        /// <summary>
InBlock.gif        
/// 判斷號碼是聯通,移動,電信中的哪個,在使用本方法前,請先驗證號碼的合法性
InBlock.gif        
/// 規則:前三位爲130-133 聯通 ;前三位爲135-139或前四位爲1340-1348 移動; 其它的應該爲電信
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="mobile">要判斷的號碼</param>
ExpandedBlockEnd.gif        
/// <returns>返回相應類型:1代表聯通;2代表移動;3代表電信</returns>

None.gif        public static int GetMobileType(string mobile)
ExpandedBlockStart.gif        
{
InBlock.gif            
if (IsChinaUnicomNumber(mobile))
InBlock.gif                
return 1 ;
InBlock.gif
InBlock.gif            
if (IsChinaMobileNumber(mobile))
InBlock.gif                
return 2 ;
InBlock.gif
InBlock.gif            
return 3 ;
ExpandedBlockEnd.gif        }

None.gif
None.gif        
//是否是聯通的號碼 測試通過
None.gif
        private static bool IsChinaUnicomNumber(string mobile)
ExpandedBlockStart.gif        
{
InBlock.gif            
string sPattern = "^(130|131|132|133)[0-9]{8}";
InBlock.gif            
bool isChinaUnicom = Regex.IsMatch(mobile,sPattern) ;
InBlock.gif
InBlock.gif            
return isChinaUnicom ;
ExpandedBlockEnd.gif        }

None.gif
None.gif        
//是否是移動的號碼 測試通過
None.gif
        private static bool IsChinaMobileNumber(string mobile) 
ExpandedBlockStart.gif        
{
InBlock.gif            
string sPattern = "^(135|136|137|138|139|1340|1341|1342|1343|1344|1345|1346|1347|1348)[1-9]{7,8}" ;
InBlock.gif
InBlock.gif            
return Regex.IsMatch(mobile,sPattern) ;
ExpandedBlockEnd.gif        }
發佈了48 篇原創文章 · 獲贊 1 · 訪問量 13萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章