读取所有行以串制[] [],直到找到下一个符号为止



我正在尝试阅读一条文本文件(每条条目一行,用两个用作属性的"枚举",请参见示例(。

[enum1_0][enum2_0]
line
line
[enum1_0][enum2_1]
line
line
[enum1_1][enum2_0]
line
line
[enum1_1][enum2_1]
line
line

我想创建一个存储所有这些字符串的string[][][],以便以后可以使用这种方法轻松地检索它们,但是我对语法有很多麻烦以及如何识别文本文件中的符号属性:

public static string GetRandomString(Enum1 e1, Enum2 e2)
{
    return _strings[(int)e1][(int)e2][_random.Next(0, length)];
}

要澄清:我有一个文本文件,每行有一个条目,其中线路块被" []" - 标记的'符号'分隔,与Myenum1和Myenum2的每个索引相对应。

我正在尝试将所有字符串读为一个字符串[],然后使用system.file.io.ReadAlllines将它们分类为字符串[] [] [] [],以便以后可以使用上面显示的函数使用我的枚举来检索它们。

因此,在适当的睡眠后,我重新考虑了我的结构,这是我的解决方案:

static List<GovermentData> _data = new List<GovermentData>();
static string[] _all = File.ReadAllLines(@"Assets/Resources/" + PREFIXES_FILEPATH + ".txt");
static string[] _nations = File.ReadAllLines(@"Assets/Resources/" + NAMES_FILEPATH + ".txt");
public static void Initialize()
{
    EconomicPolicy ep = EconomicPolicy.RadicalCollectivist;
    TechLevel tl = TechLevel.Ancient;
    //iterate all prefixes
    for (int i = 0; i < _all.Length; i++)
    {
        if (_all[i].Length <= 0)
            continue;
        if(_all[i].StartsWith("["))
        {
            string s = _all[i];
            //remove first [ and last ]
            s = s.Remove(0, 1);
            s = s.Remove(s.Length - 1, 1);
            //split on ][
            string[] r = s.Split(new string[] { "][" }, StringSplitOptions.None);
            ep = (EconomicPolicy)Enum.Parse(typeof(EconomicPolicy), r[0]);
            tl = (TechLevel)Enum.Parse(typeof(TechLevel), r[1]);
        }
        else
            _data.Add(new GovermentData(_all[i], ep, tl));
    }
}
public static string GetCivilisationName(EconomicPolicy ep, TechLevel t)
{
    GovermentData[] gd = _data.FindAll(d => d.economicPolicy == ep && d.techLevel == t).ToArray();
    if(gd.Length >= 1)
        return gd[0].name + " of " + _nations[_random.Next(0, _nations.Length)];
    else
        return gd[_random.Next(0, gd.Length)].name + " of " + _nations[_random.Next(0, _nations.Length)];
}
public struct GovermentData
{
    public readonly string name;
    public readonly EconomicPolicy economicPolicy;
    public readonly TechLevel techLevel;
    public GovermentData(string name, EconomicPolicy economicPolicy, TechLevel techLevel)
    {
        this.name = name;
        this.economicPolicy = economicPolicy;
        this.techLevel = techLevel;
    }
}

感谢您尽管有自己的困惑,但还是要帮助我。

相关内容

  • 没有找到相关文章

最新更新