我无法从列表中获取第一个项目.数据将从文本文件添加到列表中,但系统将返回System.Linq.Enumerable+<TakeIterator>d__25'1[System.String]而不是列表中的第一个项目.
以下是我的实施
string[] inputData = rawInputData.Split(',');
List<string> splitData = new List<string>(inputData.Length);
splitData.AddRange(inputData);
var numberOfCaves = splitData.Take(1);
Console.Write(numberOfCaves);
Run Code Online (Sandbox Code Playgroud)
我不确定为什么会这样,任何建议都会受到赞赏,谢谢!
所以我试图在文件中搜索一些特定的字符串,这些字符串在a中list被调用并被调用universities,我正在使用a 来加载文件.CoursesUGPGStreamreader
我遇到的问题是,在第一个foreach循环执行剩余的搜索之后,我想要完成返回,N/a就像文本文件中不存在字符串一样.但是我知道它们在文本文件中.
有没有理由这样或更好的方法来编码?
我的代码如下.
任何帮助将不胜感激.
validdirectory = new DirectoryInfo(path);
Vfiles = validdirectory.GetFiles("*.txt");
foreach (FileInfo file in Vfiles)
{
//reads the file contents
bool Stepout = false;
bool nextouterloop = false;
using (StreamReader ReadMessage = new StreamReader(file.FullName))
{
String MessageContents = ReadMessage.ReadToEnd();
Message_Viewer.Text = MessageContents;
foreach (string Uni_Name in Universities)
{
if (MessageContents.Contains(Uni_Name))
{
Display_Uni.Text = Uni_Name;
}
}
foreach (string course in Courses)
{
if (MessageContents.Contains(course))
{
Display_Course.Text …Run Code Online (Sandbox Code Playgroud) 我试图删除最后","一个字符串,但我得到一个错误说明
System.ArgumentOutOfRangeException:'StartIndex不能小于零.
字符串被写入文件正常,但在运行上述错误时它会中断.我不确定为什么会这样,任何建议都会受到赞赏.
我使用的代码如下
for (int z = 0; z <= totalNumberOfCaves; z++)
{
for (int i = 0; i < totalNumberOfCaves && connectionStack.Count > 0; i++)
{
connectionData.Add(int.Parse(connectionStack.Pop()));
}
string fileName2 = @"D:\UNI\Year 5\AI - SET09122\SET09122 - CW1\WriteConnectionData.txt";
string writeUnEditedData = "";
foreach (int s in connectionData)
{
writeUnEditedData += (s + ",");
}
using (StreamWriter sw = File.AppendText(fileName2))
{
string writeData = writeUnEditedData.Remove(writeUnEditedData.Length - 1);
sw.Write("{ " + writeData + " }," + Environment.NewLine);
}
connectionData.Clear(); …Run Code Online (Sandbox Code Playgroud)