Browse Source

增加字符串键值对解析工具

yuzhengyang 1 year ago
parent
commit
bc6a5020c7

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

@@ -94,6 +94,7 @@
     <Compile Include="DataUtils\StringUtils\StringExtension.cs" />
     <Compile Include="DataUtils\StringUtils\StringFinder.cs" />
     <Compile Include="DataUtils\StringUtils\StringGenerator.cs" />
+    <Compile Include="DataUtils\StringUtils\StringKeyValParser.cs" />
     <Compile Include="DataUtils\StringUtils\StringTool.cs" />
     <Compile Include="DataUtils\UnitConvertUtils\ByteConvertTool.cs" />
     <Compile Include="DbUtils\DbModels\DatabaseType.cs" />

+ 26 - 0
Azylee.Utils/Azylee.Core/DataUtils/StringUtils/StringKeyValParser.cs

@@ -0,0 +1,26 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Azylee.Core.DataUtils.StringUtils
+{
+    public static class StringKeyValParser
+    {
+        public static string GetValue(string s, string key, string split, string end, string defaultValue = "")
+        {
+            string head = key + split;
+            int valBegIndex = s.IndexOf(head) + head.Length;
+            if (valBegIndex >= 0)
+            {
+                int valEndIndex = s.IndexOf(end, valBegIndex);
+                if (valEndIndex >= 0)
+                {
+                    return s.Substring(valBegIndex, valEndIndex - valBegIndex);
+                }
+
+            }
+            return defaultValue;
+        }
+    }
+}

+ 21 - 5
Azylee.Utils/Tests/Test.YeahWeb/Program.cs

@@ -15,6 +15,27 @@ namespace Test.YeahWeb
     {
         static void Main(string[] args)
         {
+            testKeyValParser();
+
+        }
+
+        public static void testKeyValParser()
+        {
+            string s = "/* {{$HI.CMDST=>WAIT_BEFORE...10}} 执行脚本之前,先等待10秒钟 */\r\n/* {{$HI.CMDST=>WAIT_AFTER...10}} 执行脚本后,等待10秒钟 */";
+            string val1 = StringKeyValParser.GetValue(s, "{{$HI.CMDST=>WAIT_BEFORE", "...", "}}", "0");
+            string val2 = StringKeyValParser.GetValue(s, "{{$HI.CMDST=>WAIT_AFTER", "...", "}}", "0");
+            Console.WriteLine(val1);
+            Console.WriteLine(val2);
+        }
+
+        public static void testIp()
+        {
+            //var result = IPCNTool.Get();
+            //var rs2 = IPLocationTool.GetLocation();
+            //Console.ReadLine();
+        }
+        public static void testCusRep()
+        {
             List<string> CustRepList = new List<string>();
             CustRepList.Add("{{$HI.SYSREP=>USER.ACCOUNT1}}1");
             CustRepList.Add("{{$HI.SYSREP=>USER.ACCOUNT2}}  2");
@@ -34,11 +55,6 @@ namespace Test.YeahWeb
                 }
             }
             Console.WriteLine(keyValuePairs);
-            //var result = IPCNTool.Get();
-
-            //var rs2 = IPLocationTool.GetLocation();
-
-            //Console.ReadLine();
         }
     }
 }