获取输入字符串的位置然后在两端获取子字符串

getting position of input string then get substring on both ends

我有一个搜索功能,可以在文本块中搜索关键字并显示结果的截断版本。我的问题是,如果接近尾声,它不会显示搜索的关键字。

例如

文字="A block of text is text that is grouped together in some way, such as with the use of paragraphs or blockquotes on a Web page. Often times, the text takes on the shape of a square or rectangular block"

我用

搜索 "times"
 text = text.Substring(0, 100) + "...";

会return

"A block of text is text that is grouped together in some way, such as with the use of paragraphs or..."

有没有办法return搜索关键字前后100个字符?

你可以做到,

    string s = "A block of text is text that is grouped together in some way, such as with the use of paragraphs or";
    string toBeSearched = "grouped";
    int firstfound = s.IndexOf(toBeSearched);       
    if (firstfound != -1 )
    {
        string before = s.Substring(0 , firstfound);
        string after = s.Substring(firstfound + toBeSearched.Length);         
    }

DEMO

您也可以这样做,使其更易于重用并且能够匹配关键字的多个实例

string input = "A block of text is text that is grouped together in some way, such as with the use of paragraphs or blockquotes on a Web page. Often times, the text takes on the shape of a square or rectangular block";
int buffer = 30; // how much do you want to show before/after
string match = "times";

int location = input.IndexOf(match);
while (location != -1) {
    // take buffer before and after:
    int start = location - Math.Min (buffer , location); // don't take before start
    int end = location + match.Length 
            + Math.Min( buffer,  input.Length - location - match.Length); // don't take after end
    Console.WriteLine("..." + input.Substring(start, end-start) + "...");
    location = input.IndexOf(match,location+1);
}

为您提供

的输出
...A block of text is text that is gro...
...with the use of paragraphs or blockquotes on a Web page. Often ...
...pe of a square or rectangular block...
       string s = "A block of text is text that is grouped together in some way, such as with the use of paragraphs or";
       string wordtoSearch = "block";
       int firstfound = s.IndexOf(wordtoSearch);

        // If the index of the first letter found is greater than 100, get the 100 letters before the found word and 100 letters after the found word
        if (firstfound > 100)
        {
            string before = s.Substring(firstfound , firstfound-100);
            string after = s.Substring(firstfound + wordtoSearch.Length, 100);
            Console.WriteLine(before);
            Console.WriteLine(after);
        }
    //// If the index of the first letter found is less than 100, get the letters before the found word and 100 letters after the found word 
        if(firstfound < 100)
        {
            string before = s.Substring(0, firstfound);
            Console.WriteLine(before);
            if(s.Length >100)
            {
            string after = s.Substring(firstfound + wordtoSearch.Length, 100);
            Console.WriteLine(after);
            }
            else
            {
                string after = s.Substring(firstfound + wordtoSearch.Length);
                Console.WriteLine(after);
            }
        }