将字符串数组作为参数发送给函数

发布于 2024-12-15 03:53:00 字数 755 浏览 0 评论 0原文

我在一个名为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

颜漓半夏 2024-12-22 03:53:01

从参数中删除 []。

例如

int counter = Function.SearchedRecords(file);

,是的,您对 Contains 方法行为的假设是正确的 - 您将匹配包含五个连续破折号的任何行,无论它们之前或之后是什么字符。

如果您想解析恰好五个破折号,并且在它们之前或之后没有任何内容,我建议您查看 RegEx 类(正则表达式)。

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.

If you want to parse for exactly five dashes, with nothing before or after them I suggest looking into the RegEx class (regular expressions).

傾城如夢未必闌珊 2024-12-22 03:53:01

更改

int counter = Function.SearchedRecords( []file);

int counter = Function.SearchedRecords(file);

,是的,对于该字符串,这将起作用。
但是 Contains 区分大小写,如果您要匹配名称或另一个包含字母字符的字符串,则大小写必须相同才能匹配,例如 line.Contains("Binary Worrier") 不会匹配字符串“Hello binary worrier”。

另外,如果您知道文件总是很小,将整个文件读入内存是可以的,文件越大,此方法的效率就越低。
最好始终使用 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++;
    }
}

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".

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.

using (var reader = new System.IO.StreamReader("MyFile.txt"))
{
    while(!reader.EndOfStream)
    {
        string line = reader.ReadLine();
        if (line.Contains(pattern))
            counter++;
    }
}
如梦初醒的夏天 2024-12-22 03:53:01

将其更改为

 int counter = Function.SearchedRecords(file);

Change it to

 int counter = Function.SearchedRecords(file);
烟沫凡尘 2024-12-22 03:53:01

从方法调用中删除“[]”。是的,你的函数似乎计算你想要的东西。

Remove '[]' from a method call. Yes, your function seems to count what you want.

唯憾梦倾城 2024-12-22 03:53:01

首先,您需要创建函数类的实例,然后运行该函数。希望以下代码有帮助

Function fb = new Function();
int counter = fb.SearchedRecords(file);

现在,您正在使用 SearchRecords 作为不需要实例化的静态类的静态函数。

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.

み青杉依旧 2024-12-22 03:53:01

您可以使用 LINQ 以更短的方式完成此操作:

 int counter = file.Count(line => line.Contains("-----"));

You can do this in a shorter way using LINQ:

 int counter = file.Count(line => line.Contains("-----"));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文