I want to get the first occurence of the given word and extract the sentence before the given word charecters and after the word 40 charecters using asp.net. How i can i do it. can any one help me.
string keyword = "keyword";
string input = "there is my sentence, the keyword and the rest of the text";
int index = input.IndexOf(keyword);
string before = input.Substring(0, index);
string after = input.Substring(index + keyword.Length );
if (after.Length > 40)
{
after = after.Substring(0, 40);
}
Console.WriteLine(before);
Console.WriteLine(after);
cnu2k5
Member
20 Points
12 Posts
String manipulation
Jun 16, 2011 08:07 AM|LINK
Hi,
I want to get the first occurence of the given word and extract the sentence before the given word charecters and after the word 40 charecters using asp.net. How i can i do it. can any one help me.
shashankgwl
All-Star
18926 Points
3662 Posts
Re: String manipulation
Jun 16, 2011 08:16 AM|LINK
xplain wid a sample
All is well if it runs well.
blog
sam_xiii
Contributor
2366 Points
381 Posts
Re: String manipulation
Jun 16, 2011 08:24 AM|LINK
hi,
you can do something like this :
string keyword = "keyword"; string input = "there is my sentence, the keyword and the rest of the text"; int index = input.IndexOf(keyword); string before = input.Substring(0, index); string after = input.Substring(index + keyword.Length ); if (after.Length > 40) { after = after.Substring(0, 40); } Console.WriteLine(before); Console.WriteLine(after);http://www.sambeauvois.be