2015-10-16 47 views
1

我有一個簡歷,我想從簡歷中找到用戶的聯繫電話(手機號或電話號碼) ,需要任何想法或解決方案或任何幫助來實現目標。從給定的字符串查找電話號碼c#

我有什麼到目前爲止已經試過....

var numString = ""; 
     string strData = ",38,,,,,,,,,,,,,,,,,,,,,,,,,,,,382350,,,,,0,,,,8141884584,,,,,,,,"; 
     char[] separator = new char[] { ',' }; 
     string[] strSplitArr = strData.Split(separator); 
     for (int q = 0; q < strSplitArr.Length; q++) 
     { 
      if (strSplitArr[q] != "") 
      { 
       int no = 0; 

       no = strSplitArr[q].Length; 
       if (no >= 10 && no <= 12) 
       { 
        numString += strSplitArr[q].ToString() + ", "; 
       } 
      } 
     } 
+0

@ Izzy請參閱更新後的文章 –

+0

只需使用像''[[d] {10},' –

+0

]這樣的正則表達式模式您的示例數據看起來不像簡歷。是否有原因顯示我們正在分割CSV字符串的代碼? FYI代碼可以縮減爲'var numString = string.Join(「,」,strData.Split(',')。where(s => s.Length> = 10 && s.Length <= 12));'' – juharr

回答

2

我會建議你使用Regular Expression

這裏是一個示例代碼找到美國的電話號碼:

string text = MyInputMethod(); 
const string MatchPhonePattern = 
     @"\(?\d{3}\)?-? *\d{3}-? *-?\d{4}"; 

     Regex rx = new Regex(MatchPhonePattern, RegexOptions.Compiled | RegexOptions.IgnoreCase); 

     // Find matches. 
     MatchCollection matches = rx.Matches(text); 

     // Report the number of matches found. 
     int noOfMatches = matches.Count; 


     //Do something with the matches 

     foreach (Match match in matches) 
     { 
      //Do something with the matches 
      string tempPhoneNumber= match.Value.ToString(); ; 

     } 
+0

是否有不同的模式爲eeach國家? –

+0

這取決於電話號碼的格式。但是我也使用過其他國家的這個: '@「\(?([0-9] {3})\)?[ - 。]?([0-9] {3})[ - 。 ]?([0-9] {4})「' –

+0

好吧,讓我試一試@ S.dav –