c# - 分别读取文本文件中的每个单词

标签 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.WriteLine (line);  
       counter++;  
    } 

我需要通过计数器更改每个字符(数字)的值并以这样的方式再次保存

A = 3
B = 4
C = 1
D = 2

我想过让每个单词和=成为“第一个”,数字是“第二个”,然后循环第二个

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++;  
    } 

关于c# - 分别读取文本文件中的每个单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48473456/

相关文章:

c# - 如何快速编写和运行小型 C# 代码

asp.net - 如何自动附加VS到另一个进程进行调试?

mysql - 在 DataGrid VB.Net 中搜索(过滤器)

c# - 编译器错误 : operator "*"and"+"cannot be applied

c# - 提供程序 : Named Pipes Provider, 错误 : 40 - Could not open a connection to SQL Server, pgsql

C# - 将数据添加到列表中的列表

c# - 服务器上的 System.InvalidCastException

.net - Visual Studio 中 .NET 桌面应用程序上的 HTML5 UI

windows - 用MSVC工具生成裸机二进制文件的过程是什么?

使用 CSharpCodeProvider 进行 C# 运行时编译