将字符串数组作为参数发送给函数
我在一个名为 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;
}
我以这种方式从我的主类调用此方法:
String [] file = File.ReadAllLines("C:/Users.../results.txt");
int counter = Function.SearchedRecords( []file);
但我收到一条错误消息:
;预期
有什么问题吗?
另一个问题:上面的函数从文件中计算所有带有模式 -----
的行(即使有更多破折号,或者该行在破折号之前或之后有一些字符) )。我说得对吗?
这有点像 Java 中的模式,所以也许还有其他方法。
你能启发我吗?
I have a function in a class called Function
, like below:
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
从参数中删除 []。
例如
,是的,您对 Contains 方法行为的假设是正确的 - 您将匹配包含五个连续破折号的任何行,无论它们之前或之后是什么字符。
如果您想解析恰好五个破折号,并且在它们之前或之后没有任何内容,我建议您查看 RegEx 类(正则表达式)。
Remove the [] from your parameter.
e.g.
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.
If you want to parse for exactly five dashes, with nothing before or after them I suggest looking into the RegEx class (regular expressions).
更改
为
,是的,对于该字符串,这将起作用。
但是
Contains
区分大小写,如果您要匹配名称或另一个包含字母字符的字符串,则大小写必须相同才能匹配,例如line.Contains("Binary Worrier")
不会匹配字符串“Hello binary worrier”。另外,如果您知道文件总是很小,将整个文件读入内存是可以的,文件越大,此方法的效率就越低。
最好始终使用 System.IO.StreamReader 或 System.IO.File.ReadLines 之类的东西(在 .Net 4 及更高版本中可用),这些允许您使用文件一次一行。例如
Change
to
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".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
orSystem.IO.File.ReadLines
(available in .Net 4 and later), these allow you to consume the file one line at a time. e.g.将其更改为
Change it to
从方法调用中删除“[]”。是的,你的函数似乎计算你想要的东西。
Remove '[]' from a method call. Yes, your function seems to count what you want.
首先,您需要创建函数类的实例,然后运行该函数。希望以下代码有帮助
现在,您正在使用 SearchRecords 作为不需要实例化的静态类的静态函数。
First of all you need to create an instance of function class and then run the function. Hope following code helps
Right now, you are using SearchRecords as an static function of a static class which doesn't require instantiation.
您可以使用 LINQ 以更短的方式完成此操作:
You can do this in a shorter way using LINQ: