Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.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#如何打印以.txt文件中特定字母开头的单词_C#_Arrays_File - Fatal编程技术网

C#如何打印以.txt文件中特定字母开头的单词

C#如何打印以.txt文件中特定字母开头的单词,c#,arrays,file,C#,Arrays,File,下面我有一个方法GetWordsArray,当我在主程序中调用该方法时,它只打印出以整个句子的特定字母开头的完整句子 public static void GetWordsArray(string path, string toFind) { string[] words = File.ReadAllLines(path); if (File.Exists(path)) { foreach (string line in wo

下面我有一个方法
GetWordsArray
,当我在主程序中调用该方法时,它只打印出以整个句子的特定字母开头的完整句子

public static void GetWordsArray(string path, string toFind)
{
    string[] words = File.ReadAllLines(path);
    if (File.Exists(path)) 
    {              
        foreach (string line in words)
        {
            if(line.StartsWith(toFind))
            {
                Console.WriteLine(line);
            }                  
        }
    }
    else
    {
        Console.WriteLine("Directory not found");
    }
}
这是它从txt文件打印出来的内容:

橘子一点肉豆蔻上没有红李子。用羊肉做成的碎布一个很好的调味汁把一些朝鲜蓟的底部切碎,然后把一块五法郎的肉冻滚到一起

如果特定字母是
o
,我希望它像这样打印,例如: 橘子

这是我在主程序中调用方法的方式:

Reader r = new Reader();            
string path = @"randomtext.txt";
Reader.GetWordsArray(path, "o");

如何打印文本文件中以字母o开头的所有单词?

您的文本文件似乎只有一行,而且由于您的长句以字母o开头,它会打印整行。您可以格式化文件,使每个单词都位于一行上,或者拆分每行并迭代元素并在其中进行检查:

if (File.Exists(path))
{
    string[] words = File.ReadAllLines(path);

    foreach (string line in words)
    {
        string [] elements = line.Split(' ');
        foreach (string elem in elements)
        {
            if (elem.StartsWith(toFind))
            {
                Console.WriteLine(elem);
            }
        }
    }
}

现在,唯一的输出应该是橙子打开

,此外,您需要阅读每一行,将每一行拆分为单词,并搜索字符串中的每一个单词。您可以这样做:

    if (File.Exists(path))
    {
        string[] lines = File.ReadAllLines(path);
        foreach (var line in lines)
        {
            var words = line.Split(' ');
            foreach (var word in words)
            {
                if (word.StartsWith(toFind))
                {
                    Console.WriteLine(word);
                }
            }
        }
    }
    else
    {
        Console.WriteLine("Directory not found");
    }

在调用
File.ReadAllLines(path)
之前,也要检查
File.Exists(path)

而不是一行一行地读取整个文件。像这样:

string input = 
    @"oranges you have no red plums on a little nutmeg. RAGOUT OF LAMB A GOOD RI
    oranges you have no red plums on a little nutmeg. RAGOUT OF LAMB A GOOD RI
    oranges you have no red plums on a little nutmeg. RAGOUT OF LAMB A GOOD RI
    SSOLES Mince some artichoke-bottoms cooked by rolling the yellow asp
    ic, and throw them a five-franc piece.";
在空间与环境新行和您要查找的内容上滑动此文本:

var arrayOfWord= input.Split(  new[] { " ","\r\n", "\n" }
                             , StringSplitOptions.RemoveEmptyEntries);

string toFind = "o";
var result = arrayOfWord.Where(y=> y.StartsWith(toFind));

你现在有了你的单词列表。如果你想打印它们,一个简单的foreach就可以了。

string[]words=File.ReadAllLines(path);正在返回文件中的行数组,而不是字数组。你需要在空格和标点上分开每一行。您可能还需要一个不区分大小写的比较:它仍然打印出完整的句子x4,我只需要每个单词。有没有一种方法可以把每行中的每个单词分开?我知道了,但你打印出来的是:line而不是:elem。所以它必须是:Console.WriteLine(elem)@对不起,这是个愚蠢的复制粘贴错误。已更改。您需要首先调用:
文件。存在(路径)
的原因是什么?@meesie1文件。如果文件不存在,ReadAllLines(路径)将抛出FileNotFoundException