Skip to content

Commit 560f1a3

Browse files
author
Vadim Belov
committed
Add EnumExtensions with ToNiceString method
Introduced a static EnumExtensions class in the EasyExtensions.Extensions namespace. Added the ToNiceString extension method for Enum types to convert enum values into user-friendly strings by inserting spaces before uppercase letters. Included XML documentation for clarity and usage guidance.
1 parent 07f2945 commit 560f1a3

1 file changed

Lines changed: 38 additions & 0 deletions

File tree

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
namespace EasyExtensions.Extensions
6+
{
7+
/// <summary>
8+
/// Provides extension methods for working with enumeration (enum) values.
9+
/// </summary>
10+
/// <remarks>This static class contains utility methods that extend the functionality of enum types,
11+
/// enabling more convenient or user-friendly operations when working with enumerations.</remarks>
12+
public static class EnumExtensions
13+
{
14+
/// <summary>
15+
/// Converts the value of the specified enumeration to a user-friendly string with spaces inserted before
16+
/// uppercase letters.
17+
/// </summary>
18+
/// <remarks>This method is useful for displaying enumeration values in a more readable format,
19+
/// such as in user interfaces or logs. For example, an enum value named "OrderStatusPendingApproval" would be
20+
/// converted to "Order Status Pending Approval".</remarks>
21+
/// <param name="e">The enumeration value to convert to a nicely formatted string.</param>
22+
/// <returns>A string representation of the enumeration value with spaces inserted before uppercase letters. Returns the
23+
/// enumeration name as-is if no formatting is needed.</returns>
24+
public static string ToNiceString(this Enum e)
25+
{
26+
string enumString = e.ToString();
27+
IEnumerable<char> chars = enumString.SelectMany((c, i) =>
28+
{
29+
if (i > 0 && char.IsUpper(c))
30+
{
31+
return new char[] { ' ', c };
32+
}
33+
return new[] { c };
34+
});
35+
return new string(chars.ToArray());
36+
}
37+
}
38+
}

0 commit comments

Comments
 (0)