[英]Read all lines to string[] until next sign is found
I'm trying to read one textfile (one line per entry, separated by two "enums" used as attribute, see example).我正在尝试读取一个文本文件(每个条目一行,由用作属性的两个“枚举”分隔,参见示例)。
[enum1_0][enum2_0]
line
line
[enum1_0][enum2_1]
line
line
[enum1_1][enum2_0]
line
line
[enum1_1][enum2_1]
line
line
I want to create a string[][][]
that stores all these strings, so that I later can easily retrieve them using a method like this, but I'm having alot of trouble with the syntax and how to identify the sign attributes in the textfile:我想创建一个string[][][]
来存储所有这些字符串,以便我以后可以使用这样的方法轻松检索它们,但是我在语法和如何识别符号属性方面遇到了很多麻烦在文本文件中:
public static string GetRandomString(Enum1 e1, Enum2 e2)
{
return _strings[(int)e1][(int)e2][_random.Next(0, length)];
}
To clarify: I have a textfile that has one entry per line, where blocks of lines are separated by "[]"-marked 'signs' that correspond to every index of MyEnum1 and MyEnum2.澄清一下:我有一个文本文件,每行有一个条目,其中行块由“[]”标记的“符号”分隔,这些符号对应于 MyEnum1 和 MyEnum2 的每个索引。
I'm trying to read all strings into a single string[], and then sort them into a string[][][] using System.File.IO.ReadAllLines, so that I can later use my function shown above to retrieve them using my enums.我正在尝试将所有字符串读入一个字符串 [],然后使用 System.File.IO.ReadAllLines 将它们排序为一个字符串 [][][],以便我以后可以使用上面显示的函数来检索它们使用我的枚举。
So, after some proper sleep I reconsidered my structure, and this is my solution:因此,经过适当的睡眠后,我重新考虑了我的结构,这是我的解决方案:
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;
}
}
Thanks for trying to help me despite my own confusion.感谢您在我自己的困惑中尝试帮助我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.