Few weeks ago, I started working on a new web site project, which had one feature news section on the home page. The feature section was designed to show news title link which links to the detail page. Under the news title, I wanted to show description or at least 50 to 100 characters, but the problem was I did not want to cut any word in the middle. Therefore, I decided to create a function that will return number of words rather than number of characters.
Here is the function in C#,
public static string getLimitedWords(string str,int NumberOfWords)
{
string[] Words= str.Split(' ');
string _return=string.Empty;
if(Words.Length<=NumberOfWords)
{
_return = str;
}
else
{
for(int i=0;i<NumberOfWords;i++)
{
_return+=Words.GetValue(i).ToString()+" ";
}
}
return _return.ToString();
}
Mohammad WaheedPosted May 25, 2012, 7:06 PM
Thank you Sophea :)
Sophea BornsPosted Sep 23, 2011, 12:42 AM
I'm Born Sophea. I'm Cambodian. Your Documents is good for me to do Assignment. God bless you.
Matt AghnianejadeditedPosted Feb 5, 2009, 1:01 PMEdited Feb 5, 2009, 1:06 PM
Hi, Isn't it better to use a StringBuilder instead of a string? Thanks Matt
edralyn dulayPosted Jul 24, 2007, 4:47 AM
i was new in this program C# my Team leader wants me to revised the code of generating the customer invoice of our system. the problem was the sequence number didn't moving at all it is fix at only a number. example:1) ad10007/erd 2)ad10007/efd this sample is the problem that i was encountering as of now. i need a code for the sequence numbering example: 1)ad10007/erd 2)ad1008/egd 3)ad1009/ehf this is my problem can you help me? thank you very much sir!!!
Rakesh KumareditedPosted Feb 16, 2006, 12:33 PMEdited Feb 16, 2006, 12:35 PM
Hi, This was a good function. I want to ask for a favor if you please tell me how I can receive "NumberOfWords" in the following method public static string getLimitedWords(string str,int NumberOfWords) Thanks Rakesh