ExtensionMethods ready

This commit is contained in:
2019-01-06 12:02:58 +01:00
parent 1144ccd724
commit 786b398e26
6 changed files with 146 additions and 0 deletions

View 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))+"...";
}
}
}