我试图解析出这种具有超过8000行硬编码数据验证的单一方法。其中大部分是相同的,为数据源中的不同字段重复逻辑。如何遍历列表而其内容包含一个子值?
很多,它看起来是这样的(C++):
temp_str = _enrollment->Fields->FieldByName("ID")->AsString.SubString(1,2);
if (temp_str.IsEmpty())
{ /* do stuff */ }
else
{
if (!IsDigitsOnly(temp_str))
{ /* do different stuff */ }
else
{ /* do other stuff */ }
}
temp_str = _enrollment->Fields->FieldByName("OtherField");
if (temp_str.IsEmpty())
/* do more stuff */
所以基本上,我只需要每对temp_str = ...
之间解析出的值,然后让每个唯一验证“块”。
我目前遇到的问题是确定每个“块”的开始和结束位置。
这是我的代码:
static void Main(string[] args)
{
string file = @"C:\somePathToFile.h";
string validationHeader = "temp_str = _enrollment->Fields->FieldByName(";
string outputHeader = "=====================================================";
int startOfNextValidation;
List<string> lines = File.ReadAllLines(file).ToList<string>();
List<string> validations = new List<string>();
while (lines.Contains(validationHeader))
{
//lines[0] should be the "beginning" temp_str assignment of the validation
//lines[startOfNextValidation] should be the next temp_str assignment
startOfNextValidation = lines.IndexOf(validationHeader, lines.IndexOf(validationHeader) + 1);
//add the lines within that range to another collection
// to be iterated over and written to a textfile later
validations.Add((lines[0] + lines[startOfNextValidation]).ToString());
//remove everything up to startOfNextValidation so we can eventually exit
lines.RemoveRange(0, startOfNextValidation);
}
StreamWriter sw = File.CreateText(@"C:\someOtherPathToFile.txt");
foreach (var v in validations.Distinct())
{
sw.WriteLine(v);
sw.WriteLine(outputHeader);
}
sw.Close();
}
我while
语句不会被击中,它只是立即跳转到StreamWriter
创建,创建一个空的文本文件,因为validations
是空的。
所以我想我的第一个问题是,你如何循环检查List
,以确保在这些项目中还有包含特定“子值”的项目?
编辑:
我也试过这个;
while (lines.Where(stringToCheck => stringToCheck.Contains(validationHeader)))
通过回答; https://stackoverflow.com/a/18767402/1189566
但它说它不能从string
转换为bool
?
谢谢!这解决了我的问题,现在要弄清楚我的其他逻辑问题 – sab669