Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用C解析文件中某个位置出现的值_C# - Fatal编程技术网

C# 如何使用C解析文件中某个位置出现的值

C# 如何使用C解析文件中某个位置出现的值,c#,C#,我有字符串,每个字符串都包含一个RowKey值,存储方式如下: data-RowKey=029 这在每个文件中只发生一次。有没有什么方法可以用C函数算出数字,或者我必须自己写一些选择。我有一个队友建议linq,但我不确定这是否适用于弦乐,我不知道如何使用它 更新: 很抱歉,我将此从文件更改为字符串。Linq在这里对您没有帮助。使用正则表达式提取数字: data-Rowkey=(\d+) 更新: 代码: 林克在这里并没有真正帮助你。使用正则表达式提取数字: data-Rowkey=(\d+)

我有字符串,每个字符串都包含一个RowKey值,存储方式如下:

data-RowKey=029
这在每个文件中只发生一次。有没有什么方法可以用C函数算出数字,或者我必须自己写一些选择。我有一个队友建议linq,但我不确定这是否适用于弦乐,我不知道如何使用它

更新:


很抱歉,我将此从文件更改为字符串。

Linq在这里对您没有帮助。使用正则表达式提取数字:

data-Rowkey=(\d+)
更新:

代码:


林克在这里并没有真正帮助你。使用正则表达式提取数字:

data-Rowkey=(\d+)
更新:

代码:

假定遵循

文件必须包含与大小写完全匹配的数据行 数字长度为3 下面是代码片段

        var fileNames = Directory.GetFiles("rootDirPath");

        var tuples = new List<Tuple<String, int>>();
        foreach(String fileName in fileNames)
        {
            String fileData =File.ReadAllText(fileName) ;
            int index = fileData.IndexOf("data-RowKey=");
            if(index >=0)
            {
                String numberStr = fileData.Substring(index+12,3);// ASSUMING data-RowKey is always found, and number length is always 3
                int number = 0;
                int.TryParse(numberStr, out number);
                tuples.Add(Tuple.Create(fileName, number));
            }
        }
假定遵循

文件必须包含与大小写完全匹配的数据行 数字长度为3 下面是代码片段

        var fileNames = Directory.GetFiles("rootDirPath");

        var tuples = new List<Tuple<String, int>>();
        foreach(String fileName in fileNames)
        {
            String fileData =File.ReadAllText(fileName) ;
            int index = fileData.IndexOf("data-RowKey=");
            if(index >=0)
            {
                String numberStr = fileData.Substring(index+12,3);// ASSUMING data-RowKey is always found, and number length is always 3
                int number = 0;
                int.TryParse(numberStr, out number);
                tuples.Add(Tuple.Create(fileName, number));
            }
        }

假设它在一个文件中只存在一次,否则我甚至会抛出一个异常:

String rowKey = null;
try
{
    rowKey = File.ReadLines(path)
                .Where(l => l.IndexOf("data-RowKey=") > -1)
                .Select(l => l.Substring(12 + l.IndexOf("data-RowKey=")))
                .Single();
}
catch (InvalidOperationException) {
    // you might want to log this exception instead
    throw;
}
编辑:使用字符串的简单方法,以长度始终为3的第一次出现为例:

rowKey = text.Substring(12 + text.IndexOf("data-RowKey="), 3);

假设它在一个文件中只存在一次,否则我甚至会抛出一个异常:

String rowKey = null;
try
{
    rowKey = File.ReadLines(path)
                .Where(l => l.IndexOf("data-RowKey=") > -1)
                .Select(l => l.Substring(12 + l.IndexOf("data-RowKey=")))
                .Single();
}
catch (InvalidOperationException) {
    // you might want to log this exception instead
    throw;
}
编辑:使用字符串的简单方法,以长度始终为3的第一次出现为例:

rowKey = text.Substring(12 + text.IndexOf("data-RowKey="), 3);

你知道什么样的值总是开着吗?你知道什么样的值总是开着吗?如果它不在文件中,而只是在变量中。我该怎么做呢?字符串myVar=;匹配m=g.MatchmyVar;如果m.success{…}如果它不在文件中,而只是在变量中。我该怎么做呢?字符串myVar=;匹配m=g.MatchmyVar;如果m.Succes{…}对不起,Stefan,你能告诉我如果它只是在一个字符串变量中,我怎么做吗?例如字符串abc=。。。。。。。。。。;是源代码。对不起,Stefan,你能告诉我如果它只是一个字符串变量,我怎么做吗?例如字符串abc=。。。。。。。。。。;是源。如果是字符串而不是文件,我怎么做?如果是字符串而不是文件,我怎么做?