【问题标题】:How to read string literals and comments from a text file?如何从文本文件中读取字符串文字和注释?
【发布时间】:2009-06-17 21:43:16
【问题描述】:

我们正在从文本文件中获取输入。

每一行都在空格处分割成字符串,因此我们可以进一步对元素进行分类。 使字符串成为问题的是,我还想读取字符串文字(例如“谢谢”),因为它们也没有拆分和 cmets(// 和 /* ....*/)。有什么办法可以做到吗?

【问题讨论】:

  • 您能否提供输入文本的示例?

标签: c# .net file file-handling


【解决方案1】:

我的示例文本是:

using system.text.RegularExpressions;
namespace check_regex               //Using regex.
{
    class Program
    {
        static void Main(string[] args)
        {
            //This is a program.

            Console.WriteLine("this is a string");
            /* okay okay
              blah blah blah
              sumthng blah 908657580@#%$ */

    }
}

文本输入将是这样的。

【讨论】:

    【解决方案2】:

    执行此操作的标准方法是使用状态机 - 按顺序读取每个字符,当您看到“”时切换到“字符串”状态,并在看到时切换到“评论”状态//、/* 或 */。在每种状态下,您都可以将读取的字符附加到字符串生成器,这样就可以在输入中找到每种类型的字符串

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-28
      • 1970-01-01
      • 1970-01-01
      • 2012-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-15
      相关资源
      最近更新 更多