从文本文件中查找字符串中包含10个以上字母的所有单词

本文关键字:单词 10个 文件 文本 查找 字符串 包含 | 更新日期: 2023-09-27 18:36:41

下面我编写了代码,可以成功检查我输入的文本文件中是否包含字符"A"。它根据结果返回是或否。但是,现在我想列出所有大于 10 个字符的单词。请注意,我在读取字符串时使用ReadAllText。因此,整个文本文件位于同一字符串中。我正在寻找思考方式,而不是烤箱就绪的代码。谢谢大家!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace TESTING
{
    class Testing
    {
        static void Main(string[] args)
        {
            //ask user for the filename
            string userInput = fetchFileName("Enter the textfile you want to view: ");
            //test if the filename writes anything to console
            string fileContents = File.ReadAllText(userInput);
            string theFileContents = analyseFile(fileContents);
            //Console.WriteLine(theFileContents);
            Console.ReadLine();         
        }
       private static string analyseFile(string fileContents)
       {
            string str = fileContents;
            if (str.Contains("A"))                
            {
               Console.WriteLine("YES");                   
            }
            else
            {
                Console.WriteLine("NO");
            }
            return str;
       }
       private static string fetchFileName(string askFileName)
       {
            Console.WriteLine(askFileName);
            string userAnswer = Console.ReadLine();
            return userAnswer;
       }
    }
}

从文本文件中查找字符串中包含10个以上字母的所有单词

由于您的文件是字符串,因此您可以使用stringSplit方法将其转换为标记("单词"):

var tokens = fileContents.Split(' ', ''t', ''n', ''r');

使用标记数组,使用您希望仅保留 10 个字符的单词的过滤技术。C# 提供了许多选项来实现此目的 - 可以使用for循环、foreach循环或使用 LINQ 提供的Where扩展方法。

只需使用 String.Split(' ') 将文件内容拆分为单词,然后检查然后对返回长度为> 10 的每个单词的结果数组进行 LINQ 查询。

像这样:

string fileContents = File.ReadAllText(userInput);
var result = fileContents.Split(' ').Where(x => x.Length > 10);

这应该有效,但未经测试

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace test1
{
    class Program
    {
        static void Main(string[] args)
        {
            //ask user for the filename
            string userInput = fetchFileName("Enter the textfile you want to view: ");
            //test if the filename writes anything to console
            string fileContents = File.ReadAllText(userInput);
            string theFileContents = analyseFile(fileContents);
            //   Console.WriteLine(theFileContents);
            foreach (var item in tenOrMore(fileContents))
            {
                Console.WriteLine(item);
            }
            Console.ReadLine();
        }
        private static IEnumerable<string> tenOrMore(string fileContents)
        {
            foreach (var item in fileContents.Split(' ', ''t', ''n', ''r'))
            {
                if (item.Length.CompareTo(10) > 0)
                {
                    yield return item;
                }
            }
        }
        private static string analyseFile(string fileContents)
        {
            string str = fileContents;
            if (str.Contains("A"))
            {
                Console.WriteLine("YES");
            }
            else
            {
                Console.WriteLine("NO");
            }
            return str;
        }
        private static string fetchFileName(string askFileName)
        {
            Console.WriteLine(askFileName);
            string userAnswer = Console.ReadLine();
            return userAnswer;
        }
    }
}

使用Split如下所示:

ArrayList ar=new ArrayList(); // list of strings with the lenghth of greater than 10
String[] userInputWords=userInput.Split(' ', ''t', ''n', ''r');
foreach(String str in userInputWords){
   if (str.Length()>10){
      ar.add(str);
   }
}

另一种更天真的方法是从字符串的开头开始,每次遇到任何不是空格字符的内容时,都会递增字母计数器。

当您遇到空格时,请查看字母计数器是否> 10。如果是,则单词长度超过 10 个字符。将字母计数器重置为 0 并继续搜索单词,直到文件末尾。

这种方法的优点是不进行拆分和额外的字符串分配,这对于具有许多单词的大文件来说可能是很多工作。

伪代码:

letterCount = 0
wordCount = 0
letter = GetNextLetter()
while (isNotEndOfFile())
{
    if (notAWhitespace(letter))
    {
        letterCount++
    }
    else
    {
        if (letterCount > 10) { wordCount++ }
        letterCount=0
    }
    letter = GetNextLetter()
}
if (letterCount > 10) { wordCount++ }