繁体   English   中英

在c#中使用自定义格式检查字符串

[英]Checking string with custom format in c#

我正在为我的应用程序创建一个命令系统,但我不知道如何让 c# 识别我的字符串中的模式。

例子:

添加(变量1,变量2)

如何让 c# 识别addUser(...)并做一些事情?

我目前正在使用字符串包含,但如果用户键入add()()()()()add(((((( ,它仍然有效!

请帮忙!

对不起我的英语!

这可能并不完美,但可能会给你一些想法:)

    static void Main(string[] args)
    {
        string example_input = "xxxxx Add(user1, user2) xxxxxx";
        string myArgs = getBetween2(example_input, "Add(", ")"); //myArgs = "user1, user2"

        if (myArgs != "EMPTY")
        {
            myArgs = myArgs.Replace(" ", ""); // remove spaces... myArgs = "user1,user2"
            string[] userArray = myArgs.Split(',');

            foreach (string user in userArray)
            {
                Console.WriteLine(user);

                //Your code here
            }
        }

        Console.ReadKey(); //pause
    }

    static string getBetween2(string strSource, string strStart, string strEnd)
    {
        try
        {
            int Start, End;
            if (strSource.Contains(strStart) && strSource.Contains(strEnd))
            {
                Start = strSource.IndexOf(strStart, 0) + strStart.Length;
                End = strSource.IndexOf(strEnd, Start);
                return strSource.Substring(Start, End - Start);
            }
            else
            {
                return "EMPTY";
            }
        }
        catch
        {
            return "EMPTY";
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM