C# 分别阅读文本文件中的每个单词
我有一个文本文件(words.text)包含行,每行都有字符和类似的单词:C# 分别阅读文本文件中的每个单词,c#,visual-studio,C#,Visual Studio,我有一个文本文件(words.text)包含行,每行都有字符和类似的单词: A = 1 B = 2 C = 3 D = 4 我使用以下代码来读取文本文件 System.IO.StreamReader file = new System.IO.StreamReader(@"c:\words.txt"); while((line = file.ReadLine()) != null) { System.Console.WriteLi
A = 1
B = 2
C = 3
D = 4
我使用以下代码来读取文本文件
System.IO.StreamReader file =
new System.IO.StreamReader(@"c:\words.txt");
while((line = file.ReadLine()) != null)
{
System.Console.WriteLine (line);
counter++;
}
我需要通过计数器更改每个字符(数字)的值,并以如下方式再次保存
A = 3
B = 4
C = 1
D = 2
我想让每个单词和=成为“第一个”,数字是“第二个”,然后循环第二个
First = "A = ", Second = "1"
我不知道如何制作程序来读取每一行并识别第一行和第二行您只需将每一行的值按
=
字符分割,即可得到第一行和第二行的值:
First = "A = ", Second = "1"
System.IO.StreamReader file =
new System.IO.StreamReader(@"c:\words.txt");
while((line = file.ReadLine()) != null)
{
string[] split = line.Split('=');
string First = split[0] + " = ";
string Second = split[1];
//actually you can use split[0] and split[1], the two above llines are for demo
counter++;
}
您只需将每行值按
=
字符拆分即可获得第一个和第二个值:
System.IO.StreamReader file =
new System.IO.StreamReader(@"c:\words.txt");
while((line = file.ReadLine()) != null)
{
string[] split = line.Split('=');
string First = split[0] + " = ";
string Second = split[1];
//actually you can use split[0] and split[1], the two above llines are for demo
counter++;
}
这个怎么样
// Getting content of the file.
string contents = File.ReadAllText(@"C:\test.txt");
// Separating content
string[] contentArr = contents.Split('\n');
List<string> characterList = new List<string>();
List<int> valueList = new List<int>();
foreach (string value in contentArr)
{
characterList.Add(string.Join("", value.ToCharArray().Where(Char.IsLetter)).Trim());
valueList.Add(Int32.Parse(string.Join("", value.ToCharArray().Where(Char.IsDigit))));
}
进行更改后,可以写回文件
string output = "";
for (int i = 0; i < contentArr.Length; i++)
{
output += characterList[i] + " = " + valueList[i] + Environment.NewLine;
}
File.WriteAllText(@"C:\test.txt", output);
字符串输出=”;
for(int i=0;i
在线示例01:在线示例02:这个怎么样
// Getting content of the file.
string contents = File.ReadAllText(@"C:\test.txt");
// Separating content
string[] contentArr = contents.Split('\n');
List<string> characterList = new List<string>();
List<int> valueList = new List<int>();
foreach (string value in contentArr)
{
characterList.Add(string.Join("", value.ToCharArray().Where(Char.IsLetter)).Trim());
valueList.Add(Int32.Parse(string.Join("", value.ToCharArray().Where(Char.IsDigit))));
}
进行更改后,可以写回文件
string output = "";
for (int i = 0; i < contentArr.Length; i++)
{
output += characterList[i] + " = " + valueList[i] + Environment.NewLine;
}
File.WriteAllText(@"C:\test.txt", output);
字符串输出=”;
for(int i=0;i
在线示例01:在线示例02:您似乎在移动数字,我说的对吗?您似乎在移动数字,我说的对吗?不要忘记使用
Trim()
以避免意外的空格,特别是在这里:string First=split[0]+“=”代码>或您的替换
无效。您可以考虑做<代码>字符串第一=拆分[0 ]。代码>好,我检查过了,保存时没问题,但是换个位置怎么样??如果我需要将所有的“秒”移位3变成(A=3 B=4 C=1 D=2)??不要忘记使用Trim()
来避免意外的空格,尤其是这里:string First=split[0]+“=”代码>或您的替换
无效。您可以考虑做<代码>字符串第一=拆分[0 ]。代码>好,我检查过了,保存时没问题,但是换个位置怎么样??如果我需要将所有的“秒”移动3变成(A=3 B=4 C=1 D=2)??如果我使用文本而不是数字,那么代码是如何工作的???在第一个代码中,你必须改变这个。(第8行)列表值列表=新列表();此外,你也必须改变这一点。(第13行)valueList.Add(string.Join(“,value.tocharray().Where(Char.isleter)).Trim());在seocond代码中,您不必更改任何内容。但是,在第三个代码中,您在引号内指定了值。示例:valueList[0]=“3”;同样,在最后的代码中,你不需要改变任何东西。我写的代码如下图所示。正如你所说,如果我使用文本而不是数字,那么代码是如何工作的???在第一个代码中,你必须改变这个。(第8行)列表值列表=新列表();此外,你也必须改变这一点。(第13行)valueList.Add(string.Join(“,value.tocharray().Where(Char.isleter)).Trim());在seocond代码中,您不必更改任何内容。但是,在第三个代码中,您在引号内指定了值。示例:valueList[0]=“3”;同样在最后的代码中,你不需要改变任何东西。我用belowI编写代码,按照你说的修改它