From c177530c5ea46b35bedafc2c1d02f4ff4d86be77 Mon Sep 17 00:00:00 2001 From: Alper Ebicoglu Date: Fri, 5 Mar 2021 13:37:11 +0300 Subject: [PATCH] Handle abbreviations like XYZ when converting to camel-case. issue #7951 --- .../System/AbpStringExtensions.cs | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/framework/src/Volo.Abp.Core/System/AbpStringExtensions.cs b/framework/src/Volo.Abp.Core/System/AbpStringExtensions.cs index f272484611..5fe47112ba 100644 --- a/framework/src/Volo.Abp.Core/System/AbpStringExtensions.cs +++ b/framework/src/Volo.Abp.Core/System/AbpStringExtensions.cs @@ -260,8 +260,9 @@ namespace System /// /// String to convert /// set true to use current culture. Otherwise, invariant culture will be used. + /// set true to if you want to convert 'XYZ' to 'xyz'. /// camelCase of the string - public static string ToCamelCase(this string str, bool useCurrentCulture = false) + public static string ToCamelCase(this string str, bool useCurrentCulture = false, bool handleAbbreviations = false) { if (string.IsNullOrWhiteSpace(str)) { @@ -273,6 +274,11 @@ namespace System return useCurrentCulture ? str.ToLower() : str.ToLowerInvariant(); } + if (handleAbbreviations && IsAllUpperCase(str)) + { + return useCurrentCulture ? str.ToLower() : str.ToLowerInvariant(); + } + return (useCurrentCulture ? char.ToLower(str[0]) : char.ToLowerInvariant(str[0])) + str.Substring(1); } @@ -545,5 +551,18 @@ namespace System return encoding.GetBytes(str); } + + private static bool IsAllUpperCase(string input) + { + for (int i = 0; i < input.Length; i++) + { + if (Char.IsLetter(input[i]) && !Char.IsUpper(input[i])) + { + return false; + } + } + + return true; + } } }