2013-03-18 32 views

回答

3

最簡單的方法是用正則表達式,像這樣:

Regex = new Regex("(\d+)(years?)"); 

Match match = regex.Match(ss); 
if(match.Success) 
{ 
    string s = match.Groups[1]; 
    string s1 = match.Groups[2]; 
} 
+0

海戴夫·塞克斯頓這裏這個錯誤是顯示 – 2013-03-18 10:17:33

+0

無法識別的轉義序列系統。 Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(「(\ d +)(years?)」); – 2013-03-18 10:18:32

+0

雅在工作我用另一個\符號像(「(\\ d +(年?)」) – 2013-03-18 10:28:46

1

沒有解決辦法,但在正確的方向提示字:

  1. 在ss字符串中搜索文本字符串'years'。你會得到一個索引。 (例如21)
  2. 使用字符串ss的開始,直到索引查找數字。 (開始於21和工作方式回來)
0
string ss = "pradeep rao having 2years exp"; 

      string[] SPLIT = ss.split(' '); 

      for (int i = 0; i < SPLIT.Length; i++) 
      { 
       if (SPLIT[i].Contains("years") || SPLIT[i].Contains("year")) 
       { 
        //SPLIT[i] is the required word split it or use Substring property to get the desired result 
        break; 
       } 
      } 
+0

如何分割單詞,如果假設我在字符串中使用10年 – 2013-03-18 10:15:52