Skip to content

Commit 0c531f4

Browse files
committed
Introduced the "ToKebabCase" and "ToTrainCase" methods
1 parent 1718237 commit 0c531f4

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

src/Skybrud.Essentials/Strings/Extensions/StringExtensions.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,42 @@ public static string ToPascalCase(this string str) {
5252
public static string ToPascalCase(this Enum value) {
5353
return StringUtils.ToPascalCase(value);
5454
}
55+
56+
/// <summary>
57+
/// Converts the specified <paramref name="str"/> to a kebab cased string (lower case words separated by hyphens).
58+
/// </summary>
59+
/// <param name="str">The string to be converted.</param>
60+
/// <returns>The kebab cased string.</returns>
61+
public static string ToKebabCase(this string str) {
62+
return StringUtils.ToKebabCase(str);
63+
}
64+
65+
/// <summary>
66+
/// Converts the name of the specified enum <paramref name="value"/> to a kebab cased string (lower case words separated by hyphens).
67+
/// </summary>
68+
/// <param name="value">The enum value to be converted.</param>
69+
/// <returns>The camel cased string.</returns>
70+
public static string ToKebabCase(this Enum value) {
71+
return StringUtils.ToKebabCase(value);
72+
}
73+
74+
/// <summary>
75+
/// Converts the specified <paramref name="str"/> to a train cased string (upper case words separated by hyphens).
76+
/// </summary>
77+
/// <param name="str">The string to be converted.</param>
78+
/// <returns>The train cased string.</returns>
79+
public static string ToTrainCase(this string str) {
80+
return StringUtils.ToTrainCase(str);
81+
}
82+
83+
/// <summary>
84+
/// Converts the name of the specified enum <paramref name="value"/> to a train cased string (upper case words separated by hyphens).
85+
/// </summary>
86+
/// <param name="value">The enum value to be converted.</param>
87+
/// <returns>The camel cased string.</returns>
88+
public static string ToTrainCase(this Enum value) {
89+
return StringUtils.ToTrainCase(value);
90+
}
5591

5692
/// <summary>
5793
/// Converts the specified <paramref name="str"/> to a lower case string with words separated by underscores.

src/Skybrud.Essentials/Strings/StringUtils.cs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,60 @@ public static string ToPascalCase(Enum value) {
103103
return ToPascalCase(value.ToString());
104104
}
105105

106+
/// <summary>
107+
/// Converts the specified <paramref name="str"/> to a kebab cased string (lower case words separated by hyphens).
108+
/// </summary>
109+
/// <param name="str">The string to be converted.</param>
110+
/// <returns>The kebab cased string.</returns>
111+
public static string ToKebabCase(string str) {
112+
113+
// Replace invalid characters
114+
str = Regex.Replace(str ?? "", "[\\W_]+", " ").Trim();
115+
116+
// Replace multiple whitespaces
117+
str = Regex.Replace(str, "[ ]{2,}", " ");
118+
119+
// Convert to lower case (with upper case letters prefixed with hyphens)
120+
return Regex.Replace(str, @"(\p{Ll})(\p{Lu})", "$1-$2").Replace(" ", "-").Replace("--", "-").ToLower();
121+
122+
}
123+
124+
/// <summary>
125+
/// Converts the name of the specified enum <paramref name="value"/> to a kebab cased string (lower case words separated by hyphens).
126+
/// </summary>
127+
/// <param name="value">The enum value to be converted.</param>
128+
/// <returns>The camel cased string.</returns>
129+
public static string ToKebabCase(Enum value) {
130+
return ToKebabCase(value.ToString());
131+
}
132+
133+
/// <summary>
134+
/// Converts the specified <paramref name="str"/> to a train cased string (upper case words separated by hyphens).
135+
/// </summary>
136+
/// <param name="str">The string to be converted.</param>
137+
/// <returns>The train cased string.</returns>
138+
public static string ToTrainCase(string str) {
139+
140+
// Replace invalid characters
141+
str = Regex.Replace(str ?? "", "[\\W_]+", " ").Trim();
142+
143+
// Replace multiple whitespaces
144+
str = Regex.Replace(str, "[ ]{2,}", " ");
145+
146+
// Convert to upper case (with upper case letters prefixed with hyphens)
147+
return Regex.Replace(str, @"(\p{Ll})(\p{Lu})", "$1-$2").Replace(" ", "-").Replace("--", "-").ToUpper();
148+
149+
}
150+
151+
/// <summary>
152+
/// Converts the name of the specified enum <paramref name="value"/> to a train cased string (upper case words separated by hyphens).
153+
/// </summary>
154+
/// <param name="value">The enum value to be converted.</param>
155+
/// <returns>The camel cased string.</returns>
156+
public static string ToTrainCase(Enum value) {
157+
return ToTrainCase(value.ToString());
158+
}
159+
106160
/// <summary>
107161
/// Converts the specified <paramref name="str"/> to a lower case string with words separated by underscores.
108162
/// </summary>

0 commit comments

Comments
 (0)