2017-07-19 30 views
3

我有一個搜索功能,可以搜索文本塊中的關鍵字並顯示結果的截斷版本。我的問題是它不會顯示搜索關鍵字,如果它接近尾聲。獲取輸入字符串的位置,然後得到兩端的子字符串

例如。 「

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個字符?

+0

設置最後100個字符:'text.Substring(text.Length - 100,100)'。 'text.Substring(0,100)'對於返回前100個是正確的。 –

+0

這將適用於某些情況。然而,如果文本塊的長度爲500個字符,而搜索的關鍵字在位置100的中間呢? 這在這種情況下不起作用。 – KevinC

+0

如果你不想截斷單詞,即使單詞超過了100個字符的限制,我建議你用空格和關鍵字搜索一起使用'StringBuilder'&'Split':http://joelabrahamsson.com/c-method-for -cropping文本,不破字/。 –

回答

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); 
      } 
     } 
+1

感謝這似乎爲我工作。我修改了代碼abit ----- before = s.Substring(firstfound - 99,firstfound - (firstfound - 99)); – KevinC

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)替換'0' –

0

你可以做這樣的事情爲好,使之更有點重複使用,能夠匹配關鍵字的多個實例

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... 
相關問題