Given a string text which contains newline there is a search keyword which matches an item within the text.
给定包含换行符的字符串文本,有一个匹配文本中项目的搜索关键字。
How do I implement the following in C#:
如何在C#中实现以下功能:
searchIdx = search index (starting with 0, then 1, etc. for each successive call to GetSearchContext. Initially start with 0.
searchIdx =搜索索引(从0开始,然后是1,等等,每次连续调用GetSearchContext。最初从0开始。
contextsTxt = string data to search in
contextsTxt =要搜索的字符串数据
searchTxt = keyword to search for in contextsTxt
searchTxt =要在contextsTxt中搜索的关键字
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)
numLines =在找到的searchTxt周围返回的行数(即1 =找到searchTxt的行,2 =找到searchTxt的行,3 =找到searchTxt上方的行,searchTxt为发现,并在searchTxt下面的行找到)
returns the "context" based on the parameters
根据参数返回“上下文”
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;
}
1 个解决方案
#1
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.
它实际上不是一个家庭作业问题。我正在尝试建立一个个人搜索引擎。我刚刚想出了问题,为什么它并不总是起作用,这是由于区分大小写的搜索。
Just needed to add StringComparison.CurrentCultureIgnoreCase and voila it worked! I feel dumb for not thinking of that before posting.
只需要添加StringComparison.CurrentCultureIgnoreCase并瞧它工作了!在发布之前我没有想到这一点,我感到愚蠢。
#1
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.
它实际上不是一个家庭作业问题。我正在尝试建立一个个人搜索引擎。我刚刚想出了问题,为什么它并不总是起作用,这是由于区分大小写的搜索。
Just needed to add StringComparison.CurrentCultureIgnoreCase and voila it worked! I feel dumb for not thinking of that before posting.
只需要添加StringComparison.CurrentCultureIgnoreCase并瞧它工作了!在发布之前我没有想到这一点,我感到愚蠢。