简体   繁体   English

如何制作 substring 以便在下次出现相同单词时结束(在 C# 中)?

[英]How do I make a substring so that it ends when the next time the same word occurs(in C#)?

For example -例如 -

string text = "This is some text about a dog. The word dog \nappears in this text a number of times. This is the end";

Here, I want to create a Substring that prints till only when the word/string dog appears for the second time which is in this line The word dog \nappears in this text a number of times .在这里,我想创建一个 Substring 打印直到仅当单词/字符串dog第二次出现在此行The word dog \nappears in this text a number of times

I know the solution to this problem which is this -我知道这个问题的解决方案是 -

string text = "This is some text about a dog. The word dog appears in this text a number of times. This is the end";
            
string newText = text.Substring(0, 43);
Console.WriteLine(newText);

But what if we don't know the index/position of the same word(which is 'dog') that appears for the second time in the line.但是,如果我们不知道在该行中第二次出现的同一个词(即“狗”)的索引/位置怎么办。 What would be the code for that?那将是什么代码?

Expected Output -预计 Output -

This is some text about a dog. The word dog

Process Finished.

Here, when the word dog appears for the scond time, the string ends...在这里,当dog这个词第二次出现时,字符串就结束了……

[NOTE: You may ask for clarifications] [注意:您可以要求澄清]

Using regular expressions (aka Regex), you could solve this in a straightforward way.使用正则表达式(又名 Regex),您可以直接解决这个问题。

    var regex = new Regex("dog");
    var matches = regex.Matches(text);
    var secondIndex = matches[1].Index;

Watch out for word delimiters like spaces or punctuation marks.注意空格或标点符号等单词分隔符。 You can use [search engine of your choice] to lookup some Regex Cheat Sheet, since there is somewhat of an entry barrier to Regex.您可以使用 [您选择的搜索引擎] 查找一些 Regex 备忘单,因为 Regex 存在一定的进入障碍。

You can try using regular expresssions and Linq for this:您可以为此尝试使用正则表达式Linq

When given text and word to find当给定textword

  string text = 
    "This is some text about a dog. The word dog \nappears in this text a number of times. This is the end";

  string word = "dog";

We can find the result as我们可以找到result

  using System.Linq;
  using System.Text.RegularExpressions;  

  ...
  
  // we want 2nd match of the "dog" if it exists:
  var match = Regex
    .Matches(text, @$"\b{Regex.Escape(word)}\b", RegexOptions.IgnoreCase)
    .Cast<Match>()
    .Skip(1) // skip the 1st match
    .FirstOrDefault(); // take the second

  string result = null == match
    ? text // we don't have 2nd "dog" word at all
    : text.Substring(0, match.Index + match.Length);

  // Let's have a look at result variable
  Console.Write(result);

Outcome:结果:

This is some text about a dog. The word dog

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在C#中,如何使它像目录一样结束.aspx? 像file.aspx / programtorun / - In C#, how can I make it so it ends .aspx like a directory? Like file.aspx/programtorun/ 如何使用当用户进入文本框时,文本消失? C# - How do I make it so that when the user goes into a textbox, the text disappears? C# 从 TabControl 中删除 TabPage 时,如何在 tabcontrol 中选择下一次? C# - When removing a TabPage from the TabControl, how do I select the next time in the tabcontrol? C# 我如何制作一个可以同时处理3个用户的C#项目? - How do I make a C# project that 3 users can work on at the same time? 每当计时事件发生时,如何在表单上的ListView中刷新数据? - How do I Refresh Data inside the ListView on my form everytime the time ticker event occurs C# 如何使其与 class 位于同一命名空间中的类可以访问变量,但命名空间之外的类不在 C# 中? - How do I make it so classes in the same namespace as a class have access to a variable but classes outside of a namespace don't in C#? 当我的主程序以C#结尾时如何运行程序或函数 - How do i run a program or function when my main program ends in C# C#中的子串字 - Substring word in C# 如何同时运行控制台和 Forms? c# - How do I run Console and Forms at the same time? c# 我如何同时在两行中使用“ \\ r” c# - How do I use “\r” in 2 lines at the same time c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM