I have a function in a class called Function
, like below:
我在一个名为Function的类中有一个函数,如下所示:
public int SearchedRecords(String [] recs)
{
int counter = 0;
String pat = "-----";
String[] records = recs;
foreach (String line in records)
{
if (line.Contains(pat) == true)
{
counter++;
}
}
return counter;
}
And I am calling this method from my main class this way:
我这样从我的主类调用这个方法:
String [] file = File.ReadAllLines("C:/Users.../results.txt");
int counter = Function.SearchedRecords( []file);
But I get an error saying:
但我得到一个错误说:
;expected
;预期
What is wrong?
哪里不对?
Another question: The function above is counting from a file all the lines with the pattern -----
in them (even if with more dashes, or if the line has some chars before or after the dashes). Am I right?
另一个问题:上面的函数是从文件计数所有具有模式-----的行(即使有更多的破折号,或者如果行在破折号之前或之后有一些字符)。我对吗?
It's something like the patterns in Java so maybe there is an other way.
Can you enlighten me?
它类似于Java中的模式,所以也许还有另一种方式。你能开导我吗?
6 个解决方案
#1
4
Remove the [] from your parameter.
从参数中删除[]。
e.g.
例如
int counter = Function.SearchedRecords(file);
And yes, your assumption about the behavior of the Contains method is correct - you'll match any line containing five consecutive dashes, regardless of what characters are before or after them.
是的,您对Contains方法行为的假设是正确的 - 您将匹配包含五个连续破折号的任何行,无论它们之前或之后是什么字符。
If you want to parse for exactly five dashes, with nothing before or after them I suggest looking into the RegEx class (regular expressions).
如果你想解析五个破折号,在它们之前或之后没有任何内容,我建议查看RegEx类(正则表达式)。
#2
2
Change
更改
int counter = Function.SearchedRecords( []file);
to
至
int counter = Function.SearchedRecords(file);
and yes, this will work, for that string. However Contains
is case sensitive, if you were matching on a name, or another string with alphabetic characters, the case would have to be identical to match e.g. line.Contains("Binary Worrier")
will not match a string "Hello binary worrier".
是的,这将适用于该字符串。但是,Contains区分大小写,如果您在名称上匹配,或者使用字母字符匹配另一个字符串,则大小写必须与匹配,例如匹配。 line.Contains(“Binary Worrier”)将不匹配字符串“Hello binary worrier”。
Also, reading the entire file into memory is fine if you know that the file will always be small, this method gets less efficient the larger the file. Better to always use something like System.IO.StreamReader
or System.IO.File.ReadLines
(available in .Net 4 and later), these allow you to consume the file one line at a time. e.g.
此外,如果您知道文件总是很小,那么将整个文件读入内存是很好的,这个方法在文件越大时效率越低。最好始终使用类似System.IO.StreamReader或System.IO.File.ReadLines(在.Net 4及更高版本中可用),这些允许您一次使用一行文件。例如
using (var reader = new System.IO.StreamReader("MyFile.txt"))
{
while(!reader.EndOfStream)
{
string line = reader.ReadLine();
if (line.Contains(pattern))
counter++;
}
}
#3
1
Change it to
将其更改为
int counter = Function.SearchedRecords(file);
#4
1
Remove '[]' from a method call. Yes, your function seems to count what you want.
从方法调用中删除“[]”。是的,你的功能似乎算你想要的。
#5
1
First of all you need to create an instance of function class and then run the function. Hope following code helps
首先,您需要创建一个函数类实例,然后运行该函数。希望以下代码有帮助
Function fb = new Function();
int counter = fb.SearchedRecords(file);
Right now, you are using SearchRecords as an static function of a static class which doesn't require instantiation.
现在,您使用SearchRecords作为静态类的静态函数,不需要实例化。
#6
1
You can do this in a shorter way using LINQ:
您可以使用LINQ以更短的方式执行此操作:
int counter = file.Count(line => line.Contains("-----"));
#1
4
Remove the [] from your parameter.
从参数中删除[]。
e.g.
例如
int counter = Function.SearchedRecords(file);
And yes, your assumption about the behavior of the Contains method is correct - you'll match any line containing five consecutive dashes, regardless of what characters are before or after them.
是的,您对Contains方法行为的假设是正确的 - 您将匹配包含五个连续破折号的任何行,无论它们之前或之后是什么字符。
If you want to parse for exactly five dashes, with nothing before or after them I suggest looking into the RegEx class (regular expressions).
如果你想解析五个破折号,在它们之前或之后没有任何内容,我建议查看RegEx类(正则表达式)。
#2
2
Change
更改
int counter = Function.SearchedRecords( []file);
to
至
int counter = Function.SearchedRecords(file);
and yes, this will work, for that string. However Contains
is case sensitive, if you were matching on a name, or another string with alphabetic characters, the case would have to be identical to match e.g. line.Contains("Binary Worrier")
will not match a string "Hello binary worrier".
是的,这将适用于该字符串。但是,Contains区分大小写,如果您在名称上匹配,或者使用字母字符匹配另一个字符串,则大小写必须与匹配,例如匹配。 line.Contains(“Binary Worrier”)将不匹配字符串“Hello binary worrier”。
Also, reading the entire file into memory is fine if you know that the file will always be small, this method gets less efficient the larger the file. Better to always use something like System.IO.StreamReader
or System.IO.File.ReadLines
(available in .Net 4 and later), these allow you to consume the file one line at a time. e.g.
此外,如果您知道文件总是很小,那么将整个文件读入内存是很好的,这个方法在文件越大时效率越低。最好始终使用类似System.IO.StreamReader或System.IO.File.ReadLines(在.Net 4及更高版本中可用),这些允许您一次使用一行文件。例如
using (var reader = new System.IO.StreamReader("MyFile.txt"))
{
while(!reader.EndOfStream)
{
string line = reader.ReadLine();
if (line.Contains(pattern))
counter++;
}
}
#3
1
Change it to
将其更改为
int counter = Function.SearchedRecords(file);
#4
1
Remove '[]' from a method call. Yes, your function seems to count what you want.
从方法调用中删除“[]”。是的,你的功能似乎算你想要的。
#5
1
First of all you need to create an instance of function class and then run the function. Hope following code helps
首先,您需要创建一个函数类实例,然后运行该函数。希望以下代码有帮助
Function fb = new Function();
int counter = fb.SearchedRecords(file);
Right now, you are using SearchRecords as an static function of a static class which doesn't require instantiation.
现在,您使用SearchRecords作为静态类的静态函数,不需要实例化。
#6
1
You can do this in a shorter way using LINQ:
您可以使用LINQ以更短的方式执行此操作:
int counter = file.Count(line => line.Contains("-----"));