c#数组从具有特定值的文本文件

本文关键字:文本 文件 数组 | 更新日期: 2023-09-27 18:18:41

我有一个名为failas.txt的文件。它包含立陶宛语文本。我为它做了编码1257,这样它就可以读立陶宛字母了。

现在我所要做的就是为文件中使用的每个立陶宛字母创建一个数组。

所有这些字母都在字符串p = "AĄBCČDEĘĖFGHIĮYJKLMNOPRSŠTUŲŪVZŽ";

Array应该显示每个字母在文本中重复的次数,并将这些结果写入一个名为rezultatai.txt的新文本文件。代码是这样的:

using System;
using System.Linq;
using System.Globalization;
using System.Collections.Generic;
using System.Collections;
using System.IO; skirta biblioteka
using System.Text;
using System.Threading;
class Program 
{
static void Main()
    {
    string failas = "failas.txt"; 
    string rodymas = File.ReadAllText(failas, Encoding.GetEncoding(1257)); 
    Console.OutputEncoding = Encoding.UTF8;
    Console.WriteLine(rodymas); 
    char[] masyvas = rodymas.Where(Char.IsLetter).OrderBy(Char.ToLower).ToArray(); 
    foreach (char c in masyvas)
    {
        Console.Write(c + ",");
    }
    string p = "AĄBCČDEĘĖFGHIĮYJKLMNOPRSŠTUŲŪVZŽ";
    failas = failas.ToUpper();
    Dictionary<char, int> dict = new Dictionary<char, int>();
    foreach (char c in p) dict.Add(c, 0);
    foreach (char c in failas)
    {
        int val;
        if (dict.TryGetValue(c, out val)) dict[c] = val + 1;
    }
    //write to a file..
    foreach (KeyValuePair<char, int> item in dict)
    {
        if (item.Value > 0) Console.WriteLine("Character {0}, No of Occurences = {1}", item.Key, item.Value);
        File.AppendAllText("rezultatai.txt", item.Value + Environment.NewLine);
    }
    Console.WriteLine("Sum = {0}", dict.Sum(x => x.Value));
    Console.ReadKey();

然而,不知何故,它只返回字母A, F, I, L, S, t的输出,如下所示:

Character A, No of Occurences = 2
Character F, No of Occurences = 1
Character I, No of Occurences = 1
Character L, No of Occurences = 1
Character S, No of Occurences = 1
Character T, No of Occurences = 2

正如我之前提到的,字母应该是:

AĄBCČDEĘĖFGHIĮYJKLMNOPRSŠTUŲŪVZŽ

另外,当我打开rezultatai.txt文件检查附加值时,它只包含一长列数字:

2
0
0
0
0
0
0
0
0
1
0
0
1
0
0
0
0
1
0
0
0
0
0
1
0
2
0
0
0
0
0
0
2
0
0
0
0
0
0
0
0
1
0
0
1
0
0
0
0
1
0
0
0
0
0
1
0
2
0

c#数组从具有特定值的文本文件

然而,不知何故,它只返回字母A, F, I, L, S, t的输出,如下所示:

foreach (char c in failas)

你遍历文件名,这是"failas.txt",这应该是实际文件的文本。

foreach (char c in rodymas)
foreach (char c in masyvas)  // Possibly the char array.. not sure which..

另外,当我打开rezultatai.txt文件检查附加值时,它只包含一长列数字:

是的,您从值为整数的KeyValuePair中附加值,这可能需要与您输出到控制台的内容相同。

。. NET有非常强大的特性,比如LINQ和CultureInfo系统。您可以在几行中使用两者来完成此操作:

using System;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
internal class Program
{
  private static void Main()
  {
    var cultureLithunia = new CultureInfo("lt-LT");
    var textInfoLithunia = cultureLithunia.TextInfo;
    string requested = textInfoLithunia.ToUpper("AĄBCČDEĘĖFGHIĮYJKLMNOPRSŠTUŲŪVZŽ");
    string content = File.ReadAllText("failas.txt", Encoding.GetEncoding(textInfoLithunia.ANSICodePage));
    var characters = content.GroupBy(c => c);
    var charactersYouWant = characters.Where(c => requested.Contains(textInfoLithunia.ToUpper(c.Key)));
    var linesYouWantToOutput = charactersYouWant.Select(c => string.Format("Character {0}, No of Occurences = {1}", c.Key, c.Count()));
    File.WriteAllLines("rezultatai.txt", linesYouWantToOutput);
    Console.WriteLine("Done");
    Console.ReadKey();
  }
}

如果您想从必需的文本中获取所有字符,则有点复杂:

using System;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
internal class Program
{
  private static void Main()
  {
    var cultureLithunia = new CultureInfo("lt-LT");
    var textInfoLithunia = cultureLithunia.TextInfo;
    string requested = textInfoLithunia.ToUpper("AĄBCČDEĘĖFGHIĮYJKLMNOPRSŠTUŲŪVZŽ");
    string content = File.ReadAllText("failas.txt", Encoding.GetEncoding(textInfoLithunia.ANSICodePage));
    var characters = content.GroupBy(c => c);
    var charactersYouWant = requested.Select(c => new { Key = c, Count = characters.Where(cc => textInfoLithunia.ToUpper(cc.Key) == c).Select(group => group.Count()).FirstOrDefault() });
    var linesYouWantToOutput = charactersYouWant.Select(c => string.Format("Character {0}, No of Occurences = {1}", c.Key, c.Count));
    File.WriteAllLines("rezultatai.txt", linesYouWantToOutput);
    Console.WriteLine("Done");
    Console.ReadKey();
  }
}