Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.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#_String_While Loop_Io - Fatal编程技术网

C# 如何显示字符串

C# 如何显示字符串,c#,string,while-loop,io,C#,String,While Loop,Io,我正在编写一个搜索输入文本文件(我自己选择)的小代码。我正在创建一个搜索功能。到目前为止,我用它来显示搜索词在文本文件中出现的次数以及行号。我需要一些帮助来显示搜索的单词在文本文件中的显示方式。例如,如果我搜索单词“the”,它在文本文件中显示为“the”或“the”,我希望在控制台窗口中显示它 感谢您的任何帮助、建议或建议。提前谢谢你 这是我的密码: string line; int counter = 0; Console.WriteLine("Enter a word to search

我正在编写一个搜索输入文本文件(我自己选择)的小代码。我正在创建一个搜索功能。到目前为止,我用它来显示搜索词在文本文件中出现的次数以及行号。我需要一些帮助来显示搜索的单词在文本文件中的显示方式。例如,如果我搜索单词“the”,它在文本文件中显示为“the”或“the”,我希望在控制台窗口中显示它

感谢您的任何帮助、建议或建议。提前谢谢你

这是我的密码:

string line;
int counter = 0;

Console.WriteLine("Enter a word to search for: ");
string userText = Console.ReadLine();

string file = "NewTextFile.txt";
StreamReader myFile = new StreamReader(file);

int found = 0;

while ((line = myFile.ReadLine()) != null)
{
    counter++;
    if (line.Contains(userText))
    {
        Console.WriteLine("Found on line number: {0}", counter);
        found++;
    }
}
Console.WriteLine("A total of {0} occurences found", found);

由于要进行不区分大小写的搜索,您可以使用IndexOf而不是Contains:

if (line.IndexOf(userText, StringComparison.CurrentCultureIgnoreCase) != -1)
{
    Console.WriteLine("Found on line number: {0}", counter);
    found++;
}

这可能对你有用

While ((line = myFile.ReadLine()) != null)
{
    line = line.toUpper();
    counter++;
    if (line.Contains(userText.toUpper()))
    {
        Console.WriteLine("Found on line number: {0}", counter);
        Console.WriteLine(line.SubString(line.IndexOf(userText),userText.Length));
        found++;
    }
}
Console.WriteLine("A total of {0} occurences found", found);
这里添加的行是

line.SubString(line.IndexOf(userText),userText.Length)
这意味着我们需要在
行中找到
子字符串
,从
索引开始
第一次出现的
用户文本
用户文本长度

如果只想比较字符串并显示原始字符串,可以使用以下代码

While ((line = myFile.ReadLine()) != null)
{
    string linecmp = line.toUpper();
    counter++;
    if (linecmp.Contains(userText.toUpper()))
    {
        Console.WriteLine("Found on line number: {0}", counter);
        Console.WriteLine(line.SubString(line.IndexOf(userText),userText.Length));
        found++;
    }
}
Console.WriteLine("A total of {0} occurences found", found);

我稍微修改了你的搜索功能,以解决你以前遇到的问题。正如你在评论中提到的,你说它将“他们的”与“那个”匹配

添加以下代码以解决不匹配问题

string userText = Console.ReadLine() + " ";
修改了下面的完整代码

string line;
int counter = 0;

Console.WriteLine("Enter a word to search for: ");
string userText = Console.ReadLine() + " ";

string file = "NewTextFile.txt";
StreamReader myFile = new StreamReader(file);

int found = 0;

while((line = myFile.ReadLine()) != null)
{
    line = line.ToUpper();
    counter++;
    if (line.Contains(userText.ToUpper()))
    {
        Console.WriteLine("Found on line number: {0}", counter);
        Console.WriteLine(line.Substring(line.IndexOf(userText)+1,userText.Length));
        found++;
    }
 }

 Console.WriteLine("A total of {0} occurences found", found);
 Console.ReadLine();

希望这对您有所帮助。

非常好!这也会有帮助。这很有效!现在,我需要整合案例不敏感性。谢谢。两边都添加了
toUpper()
,使其不区分大小写。我遇到了一个问题。假设我搜索“the”这个词,它也在“they”这个词中找到它。如何绕过这个问题?因此,我添加了toUpper(),它说开始索引不能小于零。您可以看看如何绕过
他们的
问题