[英]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当给定text
和word
时
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.