C# 使用条件查找字符串数组中最长的文本片段

C# 使用条件查找字符串数组中最长的文本片段,c#,string,text,separator,string-length,C#,String,Text,Separator,String Length,晚上好。 所以我有一个文本文件,它包含多行带分隔符的文本。我需要找到最长的文本片段,条件是单词的最后一个字母必须是后面单词的第一个字母。该文本片段可以连续多行,而不仅仅是一行 F.e. 我们有这个字符串数组: Hello, obvious smile eager ruler. Rave, eyes, random. Johnny, you use. Eye eager sun. 因此,从这两行中,我们得到的文本片段将是: Hello, obvious smile eager ruler. R

晚上好。 所以我有一个文本文件,它包含多行带分隔符的文本。我需要找到最长的文本片段,条件是单词的最后一个字母必须是后面单词的第一个字母。该文本片段可以连续多行,而不仅仅是一行

F.e.

我们有这个字符串数组:

Hello, obvious smile eager ruler.
Rave, eyes, random.
Johnny, you use.
Eye eager sun.
因此,从这两行中,我们得到的文本片段将是:

Hello, obvious smile eager ruler.
Rave, eyes
Johnny, you use.
Eye eager
我们的文本片段以单词“eyes”结尾,因为“random”不是以“s”开头的。

我们的txt文件的下两行:

Hello, obvious smile eager ruler.
Rave, eyes, random.
Johnny, you use.
Eye eager sun.
因此,从这两行中,我们得到的文本片段是:

Hello, obvious smile eager ruler.
Rave, eyes
Johnny, you use.
Eye eager
我们的文本片段以单词“eager”结尾,因为“sun”不是以“r”开头的。

因此,在我们的输入文件(txt)中有多行带有分隔符的文本,我需要找到所有文本中最大的文本片段。该文本片段包含单词和分隔符

我甚至不知道从哪里开始,我想我将不得不使用诸如String.Length、Substring和String.Split之类的函数,也许Redex在这方面会很方便,但我对Redex及其函数还不是很熟悉

我尽量解释清楚,英语不是我的母语,所以有点难


我的问题是:我应该使用什么样的算法将文本拆分为单独的字符串,其中一个字符串包含一个单词,该单词后面有分隔符?

让我们创建算法:

  • 使用while循环逐行读取文件
  • 使用拆分方法拆分行,逗号作为分隔符
  • 在创建的数组中使用for循环来比较arr[i]的最后一个字符与arr[i+1]的第一个字符
  • 比较完成后,计算单词的长度
  • 将当前长度与以前的长度进行比较,并保存最长的长度和较长长度的文本片段
  • 转至步骤2以重复此工作
  • 打印文本片段

  • 您需要执行以下操作:

    String text = "Johnny, you use.\nEye eager sun.";
    
    // Splits the text into individual words
    String[] words = text.ToLower().Split(new String[] {" ", ",", ".", "\n"}, StringSplitOptions.RemoveEmptyEntries);
    
    String lastLetter = text.ToLower()[0].ToString();
    String newText = text;
    
    // Checks to see if the last letter if the previous word matches with the first letter of the next word
    foreach (String word in words)
    {
        if (word.StartsWith(lastLetter))
        {
            lastLetter = word[word.Length - 1].ToString();
        }
        else
        {
            newText = text.Split(new String[] { word }, StringSplitOptions.RemoveEmptyEntries)[0]; // Split the original text at the location where the inconsistency happens and take the first text fragment.
            break;
        }
    }
    
    Console.WriteLine(text);
    Console.WriteLine(newText);
    
    • 把课文分成几个单词
    • 比较单词的首字母和末字母,看它们是否相同
    • 如果它们不相同,请返回原始文本并在遇到该单词之前获取初始文本片段
    一种方法是:

    String text = "Johnny, you use.\nEye eager sun.";
    
    // Splits the text into individual words
    String[] words = text.ToLower().Split(new String[] {" ", ",", ".", "\n"}, StringSplitOptions.RemoveEmptyEntries);
    
    String lastLetter = text.ToLower()[0].ToString();
    String newText = text;
    
    // Checks to see if the last letter if the previous word matches with the first letter of the next word
    foreach (String word in words)
    {
        if (word.StartsWith(lastLetter))
        {
            lastLetter = word[word.Length - 1].ToString();
        }
        else
        {
            newText = text.Split(new String[] { word }, StringSplitOptions.RemoveEmptyEntries)[0]; // Split the original text at the location where the inconsistency happens and take the first text fragment.
            break;
        }
    }
    
    Console.WriteLine(text);
    Console.WriteLine(newText);
    

    你知道这些分离器吗?例如,你想要结束时间吗?分隔符是,我需要知道在哪个行和文本片段的哪个部分开始(在我们的文本片段中的第一个单词的第一个字母)以及在哪一行和它的哪一行结束(在我们的文本片段中的最后一个单词的最后一个字母),我会考虑把它分解成单词序列。查找有效的子序列,然后计算这些子序列的长度并查找最长的子序列。我知道如何将所有文本行拆分为字符串数组,该数组只包含单词,不包含分隔符,但我不知道如何拆分它,使其包含单词和分隔符,这就在这个词后面。所有发布的都是一个程序描述。然而,我们需要你去。我们不能确定你想从我们这里得到什么。请在您的帖子中加入我们可以回答的有效问题。提醒:请确保您知道,要求我们为您编写程序和建议是离题的。我认为正确的答案应该是
    去问您的讲师,告诉他们您不理解编码作业
    vs期望有人在这里为他们做工作..谢谢您的帮助!你们帮助我理解了使用/拆分字符串并比较它们的整个过程。我已经完成了所有工作,添加了更多的修改,非常感谢你们的巨大帮助@Gevo12321如果您在拆分之前使用
    .replaceAll(Pattern.compile(“[^A-Za-z0-9]”)。Pattern(),“”)
    会更好。然后你就可以按空间分割了。