c#string text,enum type

时间:2013-08-05 06:10:21

标签: c# parsing enums string-parsing

我正在阅读txt文件,我想把它分成几个部分。这个TXT文件的例子:

"Part error(1) devic[3].data_type(2)"
"Escape error(3) device[10].data_type(12)"

我想实现这样一种情况,当我有第一个单词“Part”时,我想要它的枚举类型,并且在开关中我想调用一些可以使用整行的函数,并且另一方面,当我有第一个单词“Escape”时,在开关中会有另一个用来调用其他功能的情况。我该怎么做?到目前为止,这是我的代码:

class Row
{
    public enum Category { Part, Escape }
    public string Error{ get; set; }
    public string Data_Type { get; set; }
    public string Device{ get; set; }
}

public object HandleRegex(string items)
{
        Row sk = new Row();
        Regex r = new Regex(@"[.]");
        var newStr = r.Replace(items, @" ");

        switch(this.category)
        {
            case Category.Part:
                //I want to call here function HandlePart with my line as a parameter
                HandlePart(newStr);
                break;
            case Category.Escape:
                //Here I want to call Function HandleEscape for line with "Escape" word
                HandleEscape(newStr);
                break;

        }
}

4 个答案:

答案 0 :(得分:2)

public object HandleRegex(string items)
{        
    Regex r = new Regex(@"[.]");
    var newStr = r.Replace(items, @" ");
    try {
     category = (Category) new EnumConverter(typeof(Category)).ConvertFromString(items.Split(new string[]{" "},StringSplitOptions.RemoveEmptyEntries)[0]);
        }
    catch {
       throw new ArgumentException("items doesn't contain valid prefix");
    }
    switch(category)
    {
        case Category.Part:
            HandlePart(newStr);
            break;
        case Category.Escape:
            HandleEscape(newStr);
            break;
    }
 }

答案 1 :(得分:1)

您可以使用TryParse

Category outCategory;
Enum.TryParse(this.category, out outCategory)

switch(outCategory)
        {
            case Category.Part:
                //I want to call here function HandlePart with my line as a parameter
                HandlePart(newStr);
                break;
            case Category.Escape:
                //Here I want to call Function HandleEscape for line with "Escape" word
                HandleEscape(newStr);
                break;
            default:
                // Needs to be handled
        }

答案 2 :(得分:1)

您可以创建Dictionary<Category, Action<string>>,然后根据类别使用它来调用代码:

static void Main(string[] args)
{
    var input = @"Part error(1) devic[3].data_type(2)
Escape error(3) device[10].data_type(12)";

    var functions = new Dictionary<Category, Action<string>>()
    {
        { Category.Part, HandlePart},
        { Category.Escape, HandleEscape }
    };

    foreach (var line in input.Split(new [] {Environment.NewLine }, StringSplitOptions.None))
    {
        Category category;
        if(Enum.TryParse<Category>(line.Substring(0, line.IndexOf(' ')), out category) && functions.ContainsKey(category))
            functions[category](line);
    }
}

static void HandlePart(string line)
{
    Console.WriteLine("Part handler call");
}

static void HandleEscape(string line)
{
    Console.WriteLine("Escape handler call");
}

上述程序的输出:

Part handler call
Escape handler call

答案 3 :(得分:0)

如果您逐行读取文件,则可以执行

string str = file.ReadLine();
string firstWord = str.substring(0, str.IndexOf(' ')).Trim().ToLower();

现在你已经掌握了第一个字

switch(firstWord){
  case "escape":// your code
  case "Part":// your code
}