Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从文本文件中解析此内容_C# - Fatal编程技术网

C# 如何从文本文件中解析此内容

C# 如何从文本文件中解析此内容,c#,C#,从文本文件解析下面的示例时遇到问题。我试图做的是读取整个文本文件,然后将文件拆分,使每个数字都是一个单独的元素 所以它是这样的: [466 474 482] [24 8 29] [50 46 3] 对此 [466]、[474]、[482]、[24]、[8]、[29]、[50]、[46]、[3] 我的计划是使用ReadAllLines阅读文本,然后使用split方法将数字分开,如下所示 string[] textFile = File.ReadAllLines(@"path"); strin

从文本文件解析下面的示例时遇到问题。我试图做的是读取整个文本文件,然后将文件拆分,使每个数字都是一个单独的元素

所以它是这样的:

[466 474 482]

[24 8 29]

[50 46 3]
对此

[466]、[474]、[482]、[24]、[8]、[29]、[50]、[46]、[3]

我的计划是使用ReadAllLines阅读文本,然后使用split方法将数字分开,如下所示

string[] textFile = File.ReadAllLines(@"path");
string[] textFile2 = { };

for (int i = 0; i < textFile.Length; i++)
{
     textFile2 = textFile[i].Split(' ');
}

foreach (string number in textFile2)
{
     Console.Write(number + " ");
}
string[]textFile=File.ReadAllLines(@“path”);
字符串[]textFile2={};
for(int i=0;i

但我最终得到了这个[50][46][3]。

您正在为您阅读的每一行替换
textFile2
内容。请尝试使用
列表

string[] textFile = File.ReadAllLines(@"path");
List<string> textFile2 = new List<string>();

for (int i = 0; i < textFile.Length; i++)
{
    textFile2.AddRange(textFile[i].Split(' '));
}

foreach (string number in textFile2)
{
    Console.Write(number + " ");
}
string[]textFile=File.ReadAllLines(@“path”);
List textFile2=新列表();
for(int i=0;i

如果您的文件确实是这样的,带有方括号和空行,这可能会对您有所帮助:

我更喜欢使用正则表达式来处理此类内容

确保在代码顶部调用Regex命名空间:

using System.Text.RegularExpressions;
将Regex实例定义为静态字段-如果您要频繁使用它,这是一个好主意

public static Regex extractNumberRegex = new Regex("([\\d]+)[\\s]?");
下面的示例定义了静态类中的扩展方法。 首先,让我们从字符串中提取数字

public static List<String> extractNumbers(this String source)
{
   List<String> output = new List<string>();

   MatchCollection mchCol = extractNumberRegex.Matches(source);
   foreach (Match mch in mchCol)
   {
       output.Add(mch.Value);
   }
   return output;
}
publicstaticlist-extractNumber(此字符串源)
{
列表输出=新列表();
MatchCollection mchCol=extractNumberRegex.Matches(源);
foreach(在mchCol中匹配mch)
{
输出相加(mch值);
}
返回输出;
}
您的主要方法(这里也是扩展):

公共静态列表提取(此字符串路径)
{
String[]lines=File.ReadAllLines(路径);
foreach(行中的字符串项次)
{
列表编号=ln.extractNumber();
字符串reformatLine=“”;
foreach(以数字表示的字符串数)
{
reformatLine=reformatLine+“[”+num+“]”;
}
reformatLine=reformatLine.Trim();
控制台写入线(重新格式化线);
}
}

您可以连接输入数组,然后按字符拆分

string[] textFile = File.ReadAllLines(@"path");
string concatInput = String.Join(" ", textFile);
string[] textFile2 = concatInput.Split(' ');

foreach (string number in textFile2)
{
     Console.Write(number + " ");
}
如果你想删除括号[],你可以这样做

string concatInput = String.Join(" ", textFile).Replace('[', ' ').Replace(']', ' ');
string[] textFile2 = concatInput.Split(' ').Where(x => x != "").ToArray();

textFile2每次都会被覆盖,因此它总是包含最后读取的行。请参考此链接,它不应该是
Add
处的
AddRange
?@CNuts是的!谢谢!哇,谢谢。我能问一下AddRange和Add之间的区别吗?
Add
在列表中添加一个值,而
AddRange
在列表中添加多个值吗一次。在本例中,您正在拆分
textFile[i]
,因此它将返回多个值(一个数组)。有关MSDN:vs的更多信息感谢您的解释,我还将查看链接:)
string concatInput = String.Join(" ", textFile).Replace('[', ' ').Replace(']', ' ');
string[] textFile2 = concatInput.Split(' ').Where(x => x != "").ToArray();