Files
TrainingAdvancedTechniques/ExtensionMethods/StringExtensions.cs
2019-01-06 12:02:58 +01:00

24 lines
605 B
C#

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