代码之家  ›  专栏  ›  技术社区  ›  Nick

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

  •  0
  • Nick  · 技术社区  · 8 年前

    例如,我想这样工作:

    • 显示:您好

    我的现有代码:

    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”)并在此之后显示单词的代码。

    2 回复  |  直到 8 年前
        1
  •  0
  •   Jonathan Applebaum    8 年前

    超级超基本 例如(为了更好地理解此类任务中所需的约束),使其更易于维护,并保存所有嵌套的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", "") + "*****************");
        }
    } 
    
        2
  •  0
  •   pm100    8 年前

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

    但这并不是一个真正的世界级语言口译员

    推荐文章