在 C# 中获取文本中搜索字符串的上下文 [英] Get context for search string in text in C#

查看:54
本文介绍了在 C# 中获取文本中搜索字符串的上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定包含换行符的字符串文本,有一个搜索关键字与文本中的项目匹配.

Given a string text which contains newline there is a search keyword which matches an item within the text.

我如何在 C# 中实现以下内容:

How do I implement the following in C#:

searchIdx = 搜索索引(从 0 开始,然后是 1,等等,对于每次连续调用 GetSearchContext.最初从 0 开始.

searchIdx = search index (starting with 0, then 1, etc. for each successive call to GetSearchContext. Initially start with 0.

contextsTxt = 要搜索的字符串数据

contextsTxt = string data to search in

searchTxt = 在 contextsTxt 中搜索的关键字

searchTxt = keyword to search for in contextsTxt

numLines = 围绕找到的 searchTxt 返回的行数(即 1 = 找到 searchTxt 的行,2 = 找到 searchTxt 的行,3 = 找到 searchTxt 上方的行,该行searchTxt 被找到,searchTxt 下面的那行被找到)

numLines = number of lines to return surrounding the searchTxt found (ie. 1 = the line the searchTxt is found on, 2 = the line the searchTxt is found on, 3 = the line above the searchTxt is found on, the line the searchTxt is found on, and the line below the searchTxt is found on)

根据参数返回上下文"

string GetSearchContext(int searchIdx, string contentsTxt, string searchTxt, int numLines);

string GetSearchContext(int searchIdx, string contentsTxt, string searchTxt, int numLines);

如果有更好的功能接口来实现这一点,也欢迎提出建议.

If there's a better function interface to accomplish this feel free to suggest that as well.

我尝试了以下操作,但似乎始终无法正常工作:

I tried the following but doesn't seem to work properly all the time:

    private string GetSearchContext(string contentValue, string search, int numLines)
    {
        int searchIdx = contentValue.IndexOf(search);

        int startIdx = 0;
        int lastIdx = 0;
        while (startIdx != -1 && (startIdx = contentValue.IndexOf('\n', startIdx+1)) < searchIdx)
        {
            lastIdx = startIdx;
        }

        startIdx = lastIdx;

        if (startIdx < 0)
            startIdx = 0;

        int endIdx = searchIdx;
        int lineCnt = 0;

        while (endIdx != -1 && lineCnt++ < numLines)
        {
            endIdx = contentValue.IndexOf('\n', endIdx + 1);
        }

        if (endIdx == -1 || endIdx > contentValue.Length - 1)
            endIdx = contentValue.Length - 1;

        string lines = contentValue.Substring(startIdx, endIdx - startIdx + 1);
        if (lines[0] == '\n')
            lines = lines.Substring(1);

        if (lines[lines.Length - 1] == '\n')
        {
            lines = lines.Substring(0, lines.Length - 1);
        }

        if (lines[lines.Length - 1] == '\r')
        {
            lines = lines.Substring(0, lines.Length - 1);
        }

        return lines;
    }

推荐答案

这实际上不是一个家庭作业问题.我正在尝试建立一个个人搜索引擎.我刚刚弄清楚了为什么它不总是有效的问题,这是由于区分大小写的搜索.

it's not actually a homework question. i'm trying to build a personal search engine. I just now figured out the problem as to why it didn't always work which was due to case-sensitive searching.

只需要添加 StringComparison.CurrentCultureIgnoreCase 就可以了!发帖前没有想到这一点,我感到很愚蠢.

Just needed to add StringComparison.CurrentCultureIgnoreCase and voila it worked! I feel dumb for not thinking of that before posting.

这篇关于在 C# 中获取文本中搜索字符串的上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆