简体   繁体   English

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

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

Please tell me this question answer please ... 请告诉我这个问题的答案,请...

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

in the above string i want to find the word of years(or) year and if match the the word,i wnt split the word like 在上面的字符串中,我想查找年份(或年份)的单词,如果与单词匹配,我将拆分单词,例如

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

thanks pradeep 谢谢pradeep

Easiest way would be with a regular expression like so: 最简单的方法是使用正则表达式,如下所示:

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

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

No solution but a hint in the right direction: 没有解决方案,只是朝着正确方向的提示:

  1. Search for the text string 'years' in your ss string. 在您的ss字符串中搜索文本字符串“ years”。 You will get an index. 您将获得一个索引。 (eg 21) (例如21)
  2. Use the begining of the string ss, till the index to look for the number. 使用字符串ss的开头,直到索引查找数字。 (start at 21 and work your way back) (从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