c#数组特定的字母到一个txt文件

本文关键字:一个 文件 txt 数组 | 更新日期: 2023-09-27 18:18:40

我有这个文件名为text.txt。它包含立陶宛语文本。我为它做了编码1257,所以它可以读取立陶宛字母。现在我要做的就是为文件中使用的每个立陶宛字母创建一个数组。这将显示每个字母在文本中重复了多少次,并将这些结果写入一个新的txt文件。到目前为止,我想到了这个想法:

public static int[ ] Letters(string a) {
    string p = "AĄBCČDEĘĖFGHIĮYJKLMNOPRSŠTUŲŪVZŽ"; // Lithuanian letters
    int [ ]rez = new int[p.Length];
    int ind = 0;
    foreach (char r in a ) {
       ind = p.IndexOf(r);
       if (ind>=0)
           rez[ind] ++; 
    }
    return rez;
}

我认为这将开始工作,但我没有,我不知道为什么。

c#数组特定的字母到一个txt文件

public static Dictionary<string, int> Letters(string a)
{
    Dictionary<string, int> letters = new Dictionary<string, int>();
    string p = "AĄBCČDEĘĖFGHIĮYJKLMNOPRSŠTUŲŪVZŽ"; // Lithuanian letters
    int ind = -1;
    foreach (char r in a)
    {
        ind = p.IndexOf(r);
        if (ind >= 0)
        {
            if (letters.ContainsKey(r.ToString()))
            {
                letters[r.ToString()] = letters[r.ToString()] + 1;
            }
            else
            {
                letters.Add(r.ToString(), 1);
            }
        }
    }
    return letters;
}