ExtensionMethods ready
This commit is contained in:
23
ExtensionMethods/StringExtensions.cs
Normal file
23
ExtensionMethods/StringExtensions.cs
Normal file
@ -0,0 +1,23 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
|
||||
namespace ExtensionMethods
|
||||
{
|
||||
public static class StringExtensions
|
||||
{
|
||||
public static string Shorten(this String str, int numberOfWords)
|
||||
{
|
||||
if (numberOfWords < 0)
|
||||
throw new ArgumentOutOfRangeException("numberOfWords should be greater or equal to 0.");
|
||||
if (numberOfWords == 0)
|
||||
return "";
|
||||
|
||||
var words = str.Split(' ');
|
||||
|
||||
if (words.Length <= numberOfWords)
|
||||
return str;
|
||||
|
||||
return string.Join(" ", words.Take(numberOfWords))+"...";
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user