C# 搜索文本文件中的字符串及其上一句和下一句

C# 搜索文本文件中的字符串及其上一句和下一句,c#,string,file,phrase,C#,String,File,Phrase,如果我有搜索条件:她喜欢看电视 包含一些句子的输入文件text.txt,例如: 我不知道该怎么办。她不知道这对她的健康不利。她喜欢看电视,但真的不知道说什么好。我不怪她,但这不是她的错。这只是一个测试文本。到此为止。 我想在文本文件中搜索字符串,并返回包含该字符串的句子,以及前后的句子 输出应如下所示: string @in = @"I don't know what to do. She doesn't know that it's not good for her health. S

如果我有搜索条件:
她喜欢看电视

包含一些句子的输入文件
text.txt
,例如:

我不知道该怎么办。她不知道这对她的健康不利。她喜欢看电视,但真的不知道说什么好。我不怪她,但这不是她的错。这只是一个测试文本。到此为止。

我想在文本文件中搜索字符串,并返回包含该字符串的句子,以及前后的句子

输出应如下所示:

    string @in = @"I don't know what to do. She doesn't know that it's not good for her health. She likes to watch tv but really don't know what to say. I don't blame her, but it's not her fault. This was just a test text. This is the end.";
    string phrase = @"She likes to watch tv";


    int startIndex = @in.IndexOf(phrase);
    int endIndex = startIndex + phrase.Length;
    int tmpIndex;

    tmpIndex = @in.Substring(0, startIndex).LastIndexOf(". ");
    if (tmpIndex > -1)
    {
        startIndex = tmpIndex + 1;
        tmpIndex = @in.Substring(0, startIndex).LastIndexOf(". ");
        if (tmpIndex > -1)
        {
            startIndex = tmpIndex + 1;
            tmpIndex = @in.Substring(0, startIndex).LastIndexOf(". ");
            if (tmpIndex > -1)
            {
                startIndex = tmpIndex;
            }
        }
    }

    tmpIndex = @in.IndexOf(".", endIndex);
    if (tmpIndex > -1)
    {
        endIndex = tmpIndex + 1;
        tmpIndex = @in.IndexOf(".", endIndex);
        if (tmpIndex > -1)
        {
            endIndex = tmpIndex + 1;
        }
    }

    Console.WriteLine(@in.Substring(startIndex, endIndex - startIndex).Trim());
她不知道这对她的健康不利。她喜欢看电视,但真的不知道说什么好。我不怪她,但这不是她的错。

因此,它将输出匹配搜索词前的句子、包含搜索词的句子以及搜索词后的句子。

使用
String.IndexOf()
(),它将返回文件中第一个出现的字符串。使用此值,可以删除包含的短语或句子:

int index=paration.IndexOf(“她喜欢看电视”)


然后你可以使用
索引
来设置边界和拆分(可能使用大写字母和a中的句号),将两边的句子拉出来。

像这样的事情怎么样:

    string @in = @"I don't know what to do. She doesn't know that it's not good for her health. She likes to watch tv but really don't know what to say. I don't blame her, but it's not her fault. This was just a test text. This is the end.";
    string phrase = @"She likes to watch tv";


    int startIndex = @in.IndexOf(phrase);
    int endIndex = startIndex + phrase.Length;
    int tmpIndex;

    tmpIndex = @in.Substring(0, startIndex).LastIndexOf(". ");
    if (tmpIndex > -1)
    {
        startIndex = tmpIndex + 1;
        tmpIndex = @in.Substring(0, startIndex).LastIndexOf(". ");
        if (tmpIndex > -1)
        {
            startIndex = tmpIndex + 1;
            tmpIndex = @in.Substring(0, startIndex).LastIndexOf(". ");
            if (tmpIndex > -1)
            {
                startIndex = tmpIndex;
            }
        }
    }

    tmpIndex = @in.IndexOf(".", endIndex);
    if (tmpIndex > -1)
    {
        endIndex = tmpIndex + 1;
        tmpIndex = @in.IndexOf(".", endIndex);
        if (tmpIndex > -1)
        {
            endIndex = tmpIndex + 1;
        }
    }

    Console.WriteLine(@in.Substring(startIndex, endIndex - startIndex).Trim());

我假设您要查找的短语以“.”分隔。这段代码的工作原理是查找短语的索引,查找前一个短语的匹配项,并查找后面句子的短语。

您可以使用
Regex
获取文本:

string text = "I don't know what to do. She doesn't know that it's not good for her health. She likes to watch tv but really don't know what to say. I don't blame her, but it's not her fault. This was just a test text. This is the end.";

string target = "She likes to watch tv";

string result = Regex.Replace(text, "(?:.*?\\.\\s)?((?:[^.]*?)" + target + "[^.]*?\\.)(?:.*)", "$1");

//result = "She likes to watch tv but really don't know what to say."

参考资料:

此处提供了一种方法:

string content = @"I don't know what to do. She doesn't know that it's not good for her health. She likes to watch tv but really don't know what to say. I don't blame her, but it's not her fault. This was just a test text. This is the end.";

string input = @"She likes to watch tv";
string curPhrase = string.Empty, prevPhrase = string.Empty, nextPhrase = string.Empty;

char[] delim = new char[] { '.' };
string[] phrases = content.Split(delim, StringSplitOptions.RemoveEmptyEntries);

for(int i=0; i<phrases.Length; i++){
    if(phrases[i].IndexOf(input) != -1){
        curPhrase = phrases[i];
        prevPhrase = phrases[i - 1];
        if (phrases[i + 1] != null)
            nextPhrase = phrases[i + 1];

        break;
    }
}
string content=@“我不知道该怎么办。她不知道这对她的健康不好。她喜欢看电视,但真的不知道该说什么。我不怪她,但这不是她的错。这只是一篇测试文本。到此为止。”;
字符串输入=@“她喜欢看电视”;
string curPhrase=string.Empty,prevPhrase=string.Empty,nextPhrase=string.Empty;
char[]delim=new char[]{.'.};
string[]短语=content.Split(delim、StringSplitOptions.removeMptyEntries);

对于(int i=0;iwell,我的想法是我不知道如何开始。我只是在这里找到了如何用。或!或?来分隔文本,实际上我想用“?”来分隔短语。我想我知道如何保存该短语,但不知道前后的短语。你说的“之后和之前的短语”是什么意思?你想知道这句话的整个框架还是围绕着id的框架?是的…但我想知道前后的短语,你会用regexregex做什么…嗯,你能写得更多吗?哇!谢谢你这个朋友。你救了我。还是要一步一步地弄清楚你在那里写了什么,但它起作用了!再次感谢你没有问题em!别忘了接受和/或向上投票你觉得有用的答案!这实际上是我的第一篇帖子…不能向上投票,但我想我可以接受。谢谢你的回答,但我想要“字符串短语”和前后的一个。谢谢//结果=“她不知道这对她的健康不好。她喜欢看电视,但真的不知道说什么。我不怪她,但这不是她的错。”在这种情况下,我得到了这个错误“错误1未识别的转义序列”这就是为什么我问你这是什么意思。对不起……刚才看到评论,我是新来的:)我发现以下错误:错误1“System.Array”不包含“length”的定义,并且找不到接受“System.Array”类型的第一个参数的扩展方法“length”(是否缺少using指令或程序集引用?)错误2“string”不包含“indexOf”的定义,并且找不到接受“string”类型的第一个参数的扩展方法“indexOf”(是否缺少using指令或程序集引用?)@icebox19他只需要以大写字母开始方法名。谢谢!我理解这个概念,我也喜欢它。谢谢大家!