Skip to content

Commit

Permalink
restored TruncateAtWordBoundary method
Browse files Browse the repository at this point in the history
  • Loading branch information
xavierdecoster committed May 21, 2015
1 parent e4ebefd commit 5ef3033
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/NuGetGallery/Helpers/StringExtensions.cs
@@ -1,6 +1,8 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Globalization;

namespace NuGetGallery.Helpers
{
Expand Down Expand Up @@ -34,6 +36,17 @@ public static string Abbreviate(this string text, int length)
return text.Substring(0, length - 3) + "...";
}

public static string TruncateAtWordBoundary(this string input, int length = 300, string ommission = "...", string morText = "")
{
if (string.IsNullOrEmpty(input) || input.Length < length)
return input;

int nextSpace = input.LastIndexOf(" ", length, StringComparison.Ordinal);

return string.Format(CultureInfo.CurrentCulture, "{2}{1}{0}",
morText,
ommission,
input.Substring(0, (nextSpace > 0) ? nextSpace : length).Trim());
}
}
}

0 comments on commit 5ef3033

Please sign in to comment.