簡體   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