C# 如何从一行文本中提取int?

C# 如何从一行文本中提取int?,c#,string,C#,String,我在一些数据解析中遇到了障碍,因为我正在解析的内容的标题行有点复杂。它有年份、标题和版本,但并不总是按顺序排列。年份和版本可以转换为整数,但其他不能。如果我每次都不知道整年在行中的位置,我怎么能将整年从行的其余部分拆分为int呢 示例数据集: 2016年超级特别常规赛,1月15日tossup 2013年第02轮定向题中的大量数学问题 FooBar精彩游戏第10部分第2轮03道问题2015 我知道我不能只测试整行字符是否是数字,因为有多个数字。我也不能像IndexOf那样做,因为我不能提前知道日期

我在一些数据解析中遇到了障碍,因为我正在解析的内容的标题行有点复杂。它有年份、标题和版本,但并不总是按顺序排列。年份和版本可以转换为整数,但其他不能。如果我每次都不知道整年在行中的位置,我怎么能将整年从行的其余部分拆分为int呢

示例数据集:

2016年超级特别常规赛,1月15日tossup

2013年第02轮定向题中的大量数学问题

FooBar精彩游戏第10部分第2轮03道问题2015

我知道我不能只测试整行字符是否是数字,因为有多个数字。我也不能像IndexOf那样做,因为我不能提前知道日期

要从字符串中获取所有数字,请使用regex.Matches()方法获取 正则表达式中的所有匹配项

或在一行中从第一个出现的数字中获取结果值:

现在您有了ints 1、2、3、2015

你如何知道这一年是什么取决于你。也许可以检查1900年和2017年之间的哪一个?

使用正则表达式:

    string pattern_Year = @"\(\d{4}\)";
    string pattern_Edition = @"\(\d{2}\)";
    string search = "2016 Super special regular season, 01 fifteenth tossup";
    var year = Regex.Matches(search, pattern_Year );
    var edition = Regex.Matches(search, pattern_Edition );
    if(year.Count > 0)
      Console.WriteLine(year[0].Value);
    if(edition.Count > 0)
      Console.WriteLine(edition [0].Value);
大概是这样的:

    static int GetYearFromTextLine(string s)
    {
        string [] words = s.Split(' ');

        foreach (string w in words)
        {
            int number = 0;
            if (int.TryParse(w, out number))
            {
                // assume the first number found over "1900" must be a year
                // you can modify this test yourself
                if (number >= 1900)
                {
                    return number;
                }
            }
        }
        return 0;
    }
    static void Main(string[] args)
    {
        Console.WriteLine(GetYearFromTextLine("Math problems galore 2013 Round 02 directed problems"));
    }
试试这个,应该管用

    string strValue = "abc123def456";
    char[] charArr = strValue.ToCharrArray();
    List<int> intList = new List<int>();
   for(int i =0; i < charArr.Length; i++)
      {
         string tmpInt ="";
         if(char.IsDigit(charArr[i]))
           {
            tmpInt += charArr[i];
             while((i < charArr.Lenght -1 ) && char.IsDigit([i + 1)
                 {
                   tmpInt += charArr[i+1];
                   i++;
                 }
           }
          if(tmpInt != "")
          intList.Add(int.Parse(tmpInt));
      }
string strValue=“abc123def456”;
char[]charArr=strValue.tocharraray();
List intList=新列表();
for(int i=0;i

此脚本的优点是,不管数字位于字符串中的什么位置,也不依赖于拆分或任何模式。

这回答了标题中的问题,但似乎忽略了问题正文。我已对其进行了更新。最上面的脚本准确描述了他问的问题。它从字符串中获取每个数字并将其转换为整数。这是一个非常好的解释。正则表达式在这方面只能做一些改进。
\d{4}
。这将只返回一个包含四个字符的整数。请您解释一下正则表达式模式的功能好吗?我并不经常使用它,也记不起模式中的符号是什么字符串模式“\u Year=@”(\d{4})”,很容易找到年数为4位的数字。
    string pattern_Year = @"\(\d{4}\)";
    string pattern_Edition = @"\(\d{2}\)";
    string search = "2016 Super special regular season, 01 fifteenth tossup";
    var year = Regex.Matches(search, pattern_Year );
    var edition = Regex.Matches(search, pattern_Edition );
    if(year.Count > 0)
      Console.WriteLine(year[0].Value);
    if(edition.Count > 0)
      Console.WriteLine(edition [0].Value);
    static int GetYearFromTextLine(string s)
    {
        string [] words = s.Split(' ');

        foreach (string w in words)
        {
            int number = 0;
            if (int.TryParse(w, out number))
            {
                // assume the first number found over "1900" must be a year
                // you can modify this test yourself
                if (number >= 1900)
                {
                    return number;
                }
            }
        }
        return 0;
    }
    static void Main(string[] args)
    {
        Console.WriteLine(GetYearFromTextLine("Math problems galore 2013 Round 02 directed problems"));
    }
    string strValue = "abc123def456";
    char[] charArr = strValue.ToCharrArray();
    List<int> intList = new List<int>();
   for(int i =0; i < charArr.Length; i++)
      {
         string tmpInt ="";
         if(char.IsDigit(charArr[i]))
           {
            tmpInt += charArr[i];
             while((i < charArr.Lenght -1 ) && char.IsDigit([i + 1)
                 {
                   tmpInt += charArr[i+1];
                   i++;
                 }
           }
          if(tmpInt != "")
          intList.Add(int.Parse(tmpInt));
      }