C#中的字符串操作

String Maniuplation in C#

我有一个程序可以在文本文件中找到单词并将它们打印出来。但这是一项学校作业,我需要使用一定程度的面向对象编程,比如使用不同的 classes 和接口。

所以,我遇到的问题是我有两个 public classes,当在 main class 中调用和采用时,使用 main 方法打印出我想要两个字符串值。

代码如下所示

public class GetFilePath
{
    public string FilePath;

    public GetFilePath(string fn)
    {
        /// string path = "testfile.txt";
        FilePath = fn;
    }
    public void SetFilename(string NewFilePath)
    {
        FilePath = NewFilePath;
    }   
}

public class GetSearchWord
{

    public string WordSearch;
    public GetSearchWord(string st)
    {
        WordSearch = st;
    }


    public void SetSearchTerm(string NewSearchTerm)
    {
        WordSearch = NewSearchTerm;
    }
}

这些在main函数中实现如下

        Console.Write("please enter a file to search for: ");
        // Call the constructor that has no parameters.
        GetFilePath Filepath1 = new GetFilePath("");
        Console.WriteLine(Filepath1.FilePath);
        Filepath1.SetFilename("testfile.txt");
        Console.WriteLine(Filepath1.FilePath);

        // Call the constructor that has one parameter.
        Console.Write("please enter a word to search for in the file: ");
        GetSearchWord SearchedWord1 = new GetSearchWord("");
        Console.WriteLine(SearchedWord1.WordSearch);
        SearchedWord1.SetSearchTerm("true");
        Console.WriteLine(SearchedWord1.WordSearch);

但我需要将 Filepath1.FilePathSearchedWord1.WordSearch 连接到以下字符串

string FilePath = "";
string WordSearch = "";

如您所见,目前这些都是空的。

哪些是我的搜索功能中的关键字符串,它实际上用这些词搜索行!

FilePath 和 WordSearched 字符串使用如下

using (StreamReader fs = File.OpenText(FilePath))
        {
            int count = 0; //counts the number of times wordResponse is found.
            int lineNumber = 0;
            while (!fs.EndOfStream)
            {
                string line = fs.ReadLine();
                lineNumber++;
                int position = line.IndexOf(WordSearch);
                if (position != -1)
                {
                    count++;
                    Console.WriteLine("Match#{0} line {1}: {2}", count, lineNumber, line);
                }
            }

            if (count == 0)
            {
                Console.WriteLine("your word was not found!");
            }
            else
            {
                Console.WriteLine("Your word was found " + count + " times!");
            }
            Console.WriteLine("Press enter to quit.");
            Console.ReadKey();
        }

我尝试做的是设置

string WordSearch = SearchedWord1.WordSearch;

作为我想要实现的目标的示例,SearchedWord1.WordSearch 当前设置为 "true",这是我要搜索我的文件的关键字。

如果我正确理解了你的问题,那么下面的代码应该可以解决你的问题(用以下内容更新你的主要代码):

Console.Write("please enter a file to search for: ");
        // Call the constructor that has no parameters.
        var filePathInput = Console.ReadLine();

        GetFilePath Filepath1 = new GetFilePath(filePathInput);
        Console.WriteLine(Filepath1.FilePath);
        Filepath1.SetFilename("testfile.txt");
        Console.WriteLine(Filepath1.FilePath);

        // Call the constructor that has one parameter.
        Console.Write("please enter a word to search for in the file: ");
        var searchWordInput = Console.ReadLine();
        GetSearchWord SearchedWord1 = new GetSearchWord(searchWordInput);
        Console.WriteLine(SearchedWord1.WordSearch);
        SearchedWord1.SetSearchTerm("true");
        Console.WriteLine(SearchedWord1.WordSearch);

变化是这段代码正在从用户那里获取输入...