如何找到单词并拆分字符串中的整数和字符

时间:2013-03-18 09:42:12

标签: c# string split

请告诉我这个问题的答案...

string ss="pradeep rao having 2years exp";

在上面的字符串中我想找到年(或)年的单词,如果匹配单词,我会分开单词

string s="2";
string s1="years(or)year"

感谢 普拉迪普

3 个答案:

答案 0 :(得分:3)

最简单的方法是使用正则表达式:

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

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

答案 1 :(得分:1)

没有解决方案,只是在正确的方向上提示:

  1. 在ss字符串中搜索文本字符串'years'。你会得到一个索引。 (例如21)
  2. 使用字符串ss的开头,直到索引查找数字。 (从21开始,继续前进)

答案 2 :(得分: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;
                }
            }
相关问题