c#字符串文本,枚举类型

本文关键字:枚举 类型 文本 字符串 | 更新日期: 2023-09-27 18:26:35

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

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

我想实现这样一种情况,当我有第一个单词"Part"时,我希望它有枚举类型,在switch中,我希望调用一些可以与整行一起工作的函数,另一方面,当我第一个单词为"Escape"时,switch中会有另一种情况调用其他函数。我该怎么做?这是我迄今为止的代码:

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;
        }
}

c#字符串文本,枚举类型

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;
    }
 }

您可以使用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
        }

您可以创建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

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

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

现在你有了你的第一个词你可以做

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