我正在制作console C#app,它实际上从text1中提取所有行,并在每行末尾添加一个文本“。php?”或者“。html?而且这些文本也是text2中的行,我想把text2中的第一条打印在text1中每行的末尾,然后取text2中的第二条,这样做,直到它完成text2?
下面是我的代码:
string[] resultConfig = File.ReadAllLines("keywords.txt");
string[] readParameters = File.ReadAllLines("param.txt");
for (int i = 0; i < readParameters.Length; i++)
{
for (int x = 0; x < resultConfig.Length ; x++)
{
resultConfig[x] = resultConfig[x] + readParameters[i];
Console.WriteLine(resultConfig[x]);
}
}
产出:**
keyboards.php?.html?.asp?
karachi.php?.html?.asp?
keychain.php?.html?.asp?
in khobar.php?.html?.asp?
lebanon.php?.html?.asp?
lights.php?.html?.asp?
london.php?.html?.asp?
must have.php?.html?.asp?
**
**
WHAT IT SHOULD BE:
keyboards.php?
karachi.php?
keychain.php?
in khobar.php?
lebanon.php?
lights.php?
london.php?
must have.php?
keyboards.html?
karachi.html?
keychain.html?
in khobar.html?
lebanon.html?
lights.html?
london.html?
must have.html?
**等。。
**Keywords.txt包含**
keyboards
karachi
keychain
in khobar
lebanon
lights
london
must have
**param.txt包含**
.php?
.asp?
.html?
您的问题是这行resultconfig[x]=resultconfig[x]+readparameters[i];
。在这一行中,您更改了resultconfig[x]
中的字符串,由于您使用的是嵌套循环,因此在*param.txt`文件中的每一行都发生这种情况。
为了在控制台中编写您想要的结果,请尝试以下代码:
string[] resultConfig = File.ReadAllLines("keywords.txt");
string[] readParameters = File.ReadAllLines("param.txt");
for (int i = 0; i < readParameters.Length; i++)
{
for (int x = 0; x < resultConfig.Length ; x++)
{
string line = resultConfig[x] + readParameters[i];
Console.WriteLine(line);
}
}
您不断地将参数添加到配置中,并且您应该更改循环的顺序,而不是更改数组中的值。
像这样的东西:
string[] resultConfig = File.ReadAllLines("keywords.txt");
string[] readParameters = File.ReadAllLines("param.txt");
for (int x = 0; x < resultConfig.Length ; x++)
{
for (int i = 0; i < readParameters.Length; i++)
{
Console.WriteLine(resultConfig[x] + readParameters[i]);
}
}
您似乎希望将所有这些结果保存在resultConfig
数组中,但是您不能只向数组中添加比初始化数组多的项-您必须首先使用array.resize(ref resultConfig,resultConfig.Length*ReadParameters.Length)
调整数组的大小。
但是,即使这样,追加到第一组项,然后为附加参数添加新项也会有点棘手(如果确实需要,可以这样做)。
相反,我将创建一个新的list
来保存结果,并保持初始数组原样:
string[] resultConfig =
{
"keyboards",
"karachi",
"keychain",
"in khobar",
"lebanon",
"lights",
"london",
"must have"
};
string[] readParameters = {".php?", ".html?", ".asp?"};
var allCombinations = new List<string>();
for (int i = 0; i < readParameters.Length; i++)
{
for (int x = 0; x < resultConfig.Length; x++)
{
allCombinations.Add(resultConfig[x] + readParameters[i]);
Console.WriteLine(resultConfig[x] + readParameters[i]);
}
}