diff --git a/framework/src/Volo.Abp.Core/System/AbpStringExtensions.cs b/framework/src/Volo.Abp.Core/System/AbpStringExtensions.cs index 31e7f257d4..f8086c8ecd 100644 --- a/framework/src/Volo.Abp.Core/System/AbpStringExtensions.cs +++ b/framework/src/Volo.Abp.Core/System/AbpStringExtensions.cs @@ -293,6 +293,25 @@ namespace System : Regex.Replace(str, "[a-z][A-Z]", m => m.Value[0] + " " + char.ToLowerInvariant(m.Value[1])); } + /// + /// Converts given PascalCase/camelCase string to kebab-case. + /// + /// String to convert. + /// set true to use current culture. Otherwise, invariant culture will be used. + public static string ToKebabCase(this string str, bool useCurrentCulture = false) + { + if (string.IsNullOrWhiteSpace(str)) + { + return str; + } + + str = str.ToCamelCase(); + + return useCurrentCulture + ? Regex.Replace(str, "[a-z][A-Z]", m => m.Value[0] + "-" + char.ToLower(m.Value[1])) + : Regex.Replace(str, "[a-z][A-Z]", m => m.Value[0] + "-" + char.ToLowerInvariant(m.Value[1])); + } + /// /// Converts string to enum value. /// diff --git a/framework/test/Volo.Abp.Core.Tests/System/StringExtensions_Tests.cs b/framework/test/Volo.Abp.Core.Tests/System/StringExtensions_Tests.cs index 8a818a75cb..0720a492aa 100644 --- a/framework/test/Volo.Abp.Core.Tests/System/StringExtensions_Tests.cs +++ b/framework/test/Volo.Abp.Core.Tests/System/StringExtensions_Tests.cs @@ -73,12 +73,24 @@ namespace System "Istanbul".ToCamelCase().ShouldBe("istanbul"); } + [Fact] + public void ToKebabCase_Test() + { + (null as string).ToKebabCase().ShouldBe(null); + "helloMoon".ToKebabCase().ShouldBe("hello-moon"); + "HelloWorld".ToKebabCase().ShouldBe("hello-world"); + "HelloIsparta".ToKebabCase().ShouldBe("hello-isparta"); + "ThisIsSampleText".ToKebabCase().ShouldBe("this-is-sample-text"); + } + [Fact] public void ToSentenceCase_Test() { (null as string).ToSentenceCase().ShouldBe(null); "HelloWorld".ToSentenceCase().ShouldBe("Hello world"); "HelloIsparta".ToSentenceCase().ShouldBe("Hello isparta"); + "ThisIsSampleSentence".ToSentenceCase().ShouldBe("This is sample sentence"); + "thisIsSampleSentence".ToSentenceCase().ShouldBe("this is sample sentence"); } [Fact]