繁体   English   中英

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

[英]how to find the word and split the integer and characters in a string

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

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

在上面的字符串中,我想查找年份(或年份)的单词,如果与单词匹配,我将拆分单词,例如

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

谢谢pradeep

最简单的方法是使用正则表达式,如下所示:

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

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

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

  1. 在您的ss字符串中搜索文本字符串“ years”。 您将获得一个索引。 (例如21)
  2. 使用字符串ss的开头,直到索引查找数字。 (从21开始,然后按原路返回)
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;
                }
            }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM