【问题标题】:getting position of input string then get substring on both ends获取输入字符串的位置然后在两端获取子字符串
【发布时间】:2017-07-19 02:14:57
【问题描述】:

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

例如。

Text = "文本块是以某种方式组合在一起的文本,例如在网页上使用段落或引用块。通常,文本采用正方形或矩形块的形状"

我用

搜索“时代”
 text = text.Substring(0, 100) + "...";

它会返回

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

有没有办法在搜索到的关键字前后返回100个字符?

【问题讨论】:

  • 设置最后 100 个字符:text.Substring(text.Length - 100, 100)text.Substring(0, 100) 返回前 100 个是正确的。
  • 这适用于某些情况。但是,如果文本块的长度为 500 个字符,并且搜索的关键字位于位置 100 的中间位置怎么办?在这种情况下这是行不通的。
  • 如果你不想在超过 100 个字符限制的情况下截断单词,我建议你使用StringBuilder & Split by whitespace 结合关键字搜索:joelabrahamsson.com/…。跨度>
  • @KevinC 你为什么删除答案

标签: c# string truncate


【解决方案1】:
       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);
            }
        }

【讨论】:

  • 谢谢,这似乎对我有用。我确实修改了代码 ----- before = s.Substring(firstfound - 99, firstfound - (firstfound - 99));
【解决方案2】:

你可以这样做,

    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

【讨论】:

  • 为了使其更通用,将“0”替换为:Math.Max(0, firstfound - 100)
【解决方案3】:

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

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...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多