在C#中搜索以4个字开头的单词 [英] Search a word that starts with 4 words by length in C#

查看:48
本文介绍了在C#中搜索以4个字开头的单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个代码,可以在第一列中以5个单词开头的文本文件中搜索单词

I am trying to create a code that can search words in a textfile that begin with 5 words in the first column like this

我是搜索引擎      好的,你是一个搜索引擎吗?

I am a search engine       Ok so you are a search engine eh?

第一列中的字长将用于按长度对文件进行排序。我想要在第一列中以5个单词开头的单词,不会删除任何内容。

The word length in the first column will be used to sort the file by length. Nothing is to be removed by I want words that begin with 5 words in the first column.

           如果(第一栏中的单词单词.length == 5)

            If (word in words in the first column.length ==5)

               {

              {

                    Console.WriteLine(word);

                    Console.WriteLine(word);

               }

               }

Console.Readline();

Console.Readline();

我该怎么做?这就是我尝试过的:

How can I do this? This is what I've tried:

    class Program
    {
        static void Main(string[] args)
        {



            String[] words = System.IO.File.ReadAllLines(@"C:\Users\Acer\Documents\Notes\One Two Three Four\File3.txt");


            //Get only short words

            var shortWords = from word in words where word.Length.Equals(4) select word;
            
            //Print each word out
            foreach (var word in shortWords)
            {
                if ((word.StartsWith(word.Length) = 5))
                {
                    Console.WriteLine(word);
                }
            }
            Console.ReadLine();
        }

推荐答案

你的意思是什么?标准文本文件中没有列。它只是由换行符分隔的一堆单词。

What do you mean by column? There are no columns in a standard text file. It is just a bunch of words separated by newlines.

对于你的代码,它似乎没有做与你问的问题有关的任何事情。在读完你的文本行之后,然后枚举每一行并查看该行是否正好是4个字符长。如果它是那么你正在考虑一个简短的词。
不确定你想要达到的逻辑。如果你试图消除常用词(即a,a,那么),那么你可能会考虑使用模糊搜索库。 

For your code it doesn't seem to be doing anything related to the question you're asking. After read the lines of text in you're then enumerating each line and seeing if the line is exactly 4 characters long. If it is then you're considering it a short word. Not really sure the logic you're trying to get at here. If you're trying to eliminate common words (i.e. an, a, the) then you might consider using a fuzzy search library instead. 

在你的foreach循环中,代码没有看起来它会编译。您尝试实现的逻辑尚不清楚,因为该代码正在尝试确定该行(正好是4个字符长)是否以
行后出现的内容开头,并将所有内容与5进行比较.StartsWith要求一个字符串,所以你想要确定1个字符串是否以其他字符串开头时使用它。它返回true或false,因此比较为5(实际上你在这里使用赋值)将永远不会
工作,即使它已编译。

Inside your foreach loop that code doesn't look like it would compile. The logic you're trying to implement is unclear because that code is trying to determine if the line (that is exactly 4 characters long) starts with something that would appear after the line and comparing all that to 5. StartsWith requires a string so you'd use it when you want to determine if 1 string starts with some other string. It returns back true or false so the comparison to 5 (actually you're using assignment here) would never work, even if it did compile.

Michael Taylor
http://www.michaeltaylorp3.net

Michael Taylor
http://www.michaeltaylorp3.net


这篇关于在C#中搜索以4个字开头的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆