Browse Source

命名工具(驼峰-下划线)

于正洋 3 years ago
parent
commit
066e131a39

+ 1 - 0
Azylee.Utils/Azylee.Core/Azylee.Core.csproj

@@ -84,6 +84,7 @@
     <Compile Include="DataUtils\EnumUtils\FlagsEnumTool.cs" />
     <Compile Include="DataUtils\GuidUtils\GuidTool.cs" />
     <Compile Include="DataUtils\SerializeUtils\SerializeTool.cs" />
+    <Compile Include="DataUtils\StringUtils\NameFormat.cs" />
     <Compile Include="DataUtils\StringUtils\Str.cs" />
     <Compile Include="DataUtils\StringUtils\StringArrayTool.cs" />
     <Compile Include="DataUtils\StringUtils\StringExtension.cs" />

+ 94 - 0
Azylee.Utils/Azylee.Core/DataUtils/StringUtils/NameFormat.cs

@@ -0,0 +1,94 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Azylee.Core.DataUtils.StringUtils
+{
+    public class NameFormat
+    {
+        public static string ToCamelCase(string s)
+        {
+            string result = "";
+            if (Str.Ok(s))
+            {
+                if (s.IndexOf('_') >= 0)
+                {
+                    bool upFlag = false;
+                    char[] cArray = s.ToArray();
+                    foreach (var c in cArray)
+                    {
+                        if (c == '_')
+                        {
+                            upFlag = true;
+                            continue;
+                        }
+
+                        if (upFlag)
+                        {
+                            result += c.ToString().ToUpper();
+                            upFlag = false;
+                        }
+                        else
+                        {
+                            result += c.ToString().ToLower();
+                        }
+                    }
+                }
+                else
+                {
+                    result = s;
+                }
+            }
+            return result;
+        }
+        public static string ToUpCamelCase(string s)
+        {
+            string result = "";
+            if (Str.Ok(s))
+            {
+                bool upFlag = false;
+                char[] cArray = s.ToArray();
+                for (int i = 0; i < cArray.Length; i++)
+                {
+                    char c = cArray[i];
+                    if (c == '_')
+                    {
+                        upFlag = true;
+                        continue;
+                    }
+
+                    if (upFlag || i == 0)
+                    {
+                        result += c.ToString().ToUpper();
+                        upFlag = false;
+                    }
+                    else
+                    {
+                        result += c.ToString().ToLower();
+                    }
+                }
+            }
+            return result;
+        }
+        public static string ToUnderline(string s)
+        {
+            string result = "";
+            if (Str.Ok(s))
+            {
+                char[] cArray = s.ToArray();
+                foreach (var c in cArray)
+                {
+                    char cUpper = char.ToUpper(c);
+                    char cLower = char.ToLower(c);
+                    if (c >= 'A' && c <= 'Z')
+                    {
+                        result += "_";
+                    }
+                    result += char.ToLower(c);
+                }
+            }
+            return result;
+        }
+    }
+}