如何在 c# 中搜索字符串中的关键字并显示没有关键字的字符串?

How to search string for keyword and display string without keyword in c#?

我目前正在使用 C# 开发我自己的编程语言,但我在尝试做某事时遇到了问题。我已经弄清楚了我需要在哪里读取代码并将其存储在字符串中。但是,我的问题是,我试图在搜索关键字的地方使用它并显示没有关键字的那一行。 例如,我想这样工作:

我无法让它在 C# 中工作。 我现有的代码:

public void Run()
    {
        string uriPath;
        uriPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
        string path = new Uri(uriPath).LocalPath;
        NBasicInfo info = new NBasicInfo();
        Console.Clear();
        Console.WriteLine("Run Project");
        Console.WriteLine("=======================================");
        Console.WriteLine("Files to open: ");
        string[] filePaths = Directory.GetFiles(path + @"\NDOS\Programs\NBasic");
        for (int i = 0; i < filePaths.Length; ++i)
        {
            string _path = filePaths[i];
            Console.WriteLine(System.IO.Path.GetFileName(_path));
         }
         Console.WriteLine();
         Console.Write("Project Name (Name before .nbasic) (Do not include .nbasic): ");
         string fileName = Console.ReadLine();
         try
         {
            StreamReader contentCode = new StreamReader(path + @"\NDOS\Programs\NBasic\" + fileName + ".nbasic");
            string codeContent = contentCode.ReadToEnd();
            contentCode.Close();
            XmlSerializer xs = new XmlSerializer(typeof(NBasicInfo));
            FileStream read = new FileStream(path + @"\NDOS\Programs\NBasic\" + fileName + ".xml", FileMode.Open, FileAccess.Read, FileShare.Read);
            NBasicInfo info2 = (NBasicInfo)xs.Deserialize(read);
            info2.pname = info2.ProjectName;
            info2.aname = info2.ApplicationName;
            info2.dname = info2.DeveloperName;
            read.Close();
            Console.Clear();
            Console.WriteLine("Running " + info2.aname + "...");
            Console.WriteLine("===================================================================");
            Thread.Sleep(200);
            Console.ReadKey();
         }
         catch
         {
            Console.WriteLine("Error running code. Please check code and correct any mistakes.");
         }
    }

我需要的是搜索关键字(在本例中为 'print')并显示之后的词的代码。

我可以给你一个简单的答案

if(command.StartsWith("Print"))
{
  Console.WriteLine(command.Substring(6));
}

但这并不是真正的世界级语言翻译

你说你正在开发一种新的编程语言...这里有一个 super ultra basic 示例(为了更好地理解此类任务中所需的约束)来做更多可维护并保存所有嵌套的 if 声明,如@Keith Nicholas 所述:

class Program
{
    static void Main(string[] args)
    {
        PrintKeyWords.CheckForKeyWord("print hello world!!!");
        PrintKeyWords.CheckForKeyWord("specialPrint hello world!!!");
        Console.ReadLine();
    }
}

delegate void ExecuteMethod();
public static class PrintKeyWords
{

    private static string Command { get; set; }
    public static void CheckForKeyWord(string cmd)
    {

        foreach (KeyValuePair<string, ExecuteMethod> keyword in KeyWordsDict)
        {
            if (cmd.StartsWith(keyword.Key))
            {
                Command = cmd;
                KeyWordsDict[keyword.Key].Invoke();
                return;
            }
        }
            Console.WriteLine("your command is not recognized as an internal or exter
    }

    private static Dictionary<string, ExecuteMethod> _KeyWordsDict;
    private static Dictionary<string, ExecuteMethod> KeyWordsDict
    {
        get
        {
            if (_KeyWordsDict == null)
            {
                _KeyWordsDict = new Dictionary<string, ExecuteMethod>();
                _KeyWordsDict.Add("print", Print);
                _KeyWordsDict.Add("specialPrint", SpecialPrint);
                return _KeyWordsDict;
            }

            else
            {
                return _KeyWordsDict;
            }
        }
    }

    private static void Print()
    {
        Console.WriteLine(Command.Replace("print", ""));
    }

    private static void SpecialPrint()
    {
        Console.WriteLine(Command.Replace("specialPrint", "") + "*****************");
    }
}