C# 获取输入字符串的位置,然后获取两端的子字符串
我有一个搜索功能,可以搜索文本块中的关键字,并显示结果的截断版本。我的问题是,它不会显示搜索的关键字,如果它接近尾声 比如说 Text=“文本块是以某种方式组合在一起的文本,例如在网页上使用段落或块引号。通常情况下,文本呈现方形或矩形块的形状” 我搜索“时代”与 它会回来的C# 获取输入字符串的位置,然后获取两端的子字符串,c#,string,truncate,C#,String,Truncate,我有一个搜索功能,可以搜索文本块中的关键字,并显示结果的截断版本。我的问题是,它不会显示搜索的关键字,如果它接近尾声 比如说 Text=“文本块是以某种方式组合在一起的文本,例如在网页上使用段落或块引号。通常情况下,文本呈现方形或矩形块的形状” 我搜索“时代”与 它会回来的 "A block of text is text that is grouped together in some way, such as with the use of paragraphs or..." 有没有办法在
"A block of text is text that is grouped together in some way, such as with the use of paragraphs or..."
有没有办法在搜索的关键字前后返回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);
}
您也可以这样做,使其更易于重用,并且能够匹配关键字的多个实例
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=“文本块是以某种方式组合在一起的文本,例如使用段落或”;
字符串wordtoSearch=“block”;
int firstfound=s.IndexOf(wordtoSearch);
//如果找到的第一个字母的索引大于100,则获取找到的单词前的100个字母和找到的单词后的100个字母
如果(首次发现>100)
{
字符串before=s.Substring(firstfound,firstfound-100);
后面的字符串=s.Substring(firstfound+wordtoSearch.Length,100);
控制台写入线(之前);
控制台写入线(后);
}
////如果找到的第一个字母的索引小于100,则获取找到的单词前的字母和找到的单词后的100个字母
如果(首次发现<100)
{
字符串before=s.Substring(0,firstfound);
控制台写入线(之前);
如果(s.长度>100)
{
后面的字符串=s.Substring(firstfound+wordtoSearch.Length,100);
控制台写入线(后);
}
其他的
{
后面的字符串=s.Substring(firstfound+wordtoSearch.Length);
控制台写入线(后);
}
}
设置最后100个字符:text.Substring(text.Length-100100)
<代码>文本。子字符串(01100)对于返回前100个是正确的。这在某些情况下适用。然而,如果文本块长500个字符,并且搜索关键字位于位置100的中间,该怎么办?在这种情况下,这不起作用。如果您不想在单词超过100个字符限制的情况下截断单词,我建议您使用StringBuilder
&Split
和关键字搜索:。@KevinC为什么要删除答案为了使其更通用,请将“0”替换为:Math.Max(0,firstfind-100)谢谢,这似乎对我有用。我确实修改了代码abit---before=s.Substring(firstfound-99,firstfound-(firstfound-99));
...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);
}
}