小编teq*_*ras的帖子

Dictionary <string,int>增加值

我有一个Dictionary<string, int>,我正在从列表中读取一些字符串...我想在字典中添加它们,但如果字符串已经在字典中,我希望它的值增加1.

我尝试的代码如下所示,但是有一些字符串随着每个输入而增加..是不是有问题?

    Dictionary<string, int> dictionary = new Dictionary<string, int>();
    foreach (String recordline in tags)
    {
        String recordstag = recordline.Split('\t')[1];
        String tagToDic = recordstag.Substring(0, (recordstag.Length-1) );

        if (dictionary.ContainsKey(tagToDic) == false)
        {
            dictionary.Add(tagToDic, 1);
        }
        else
        {

            try
            {
                dictionary[tagToDic] = dictionary[tagToDic] + 1;
            }
            catch (KeyNotFoundException ex)
            {
                System.Console.WriteLine("X" + tagToDic + "X");
                dictionary.Add(tagToDic, 1);
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

编辑:回答你的评论...我删除字符串的最后一个字符,因为它总是一个空格...我的输入如下:

10000301    business    0   0,000
10000301    management & auxiliary services     0   0,000
10000316    demographie     0   0,000
10000316    histoire de france …
Run Code Online (Sandbox Code Playgroud)

c# dictionary

4
推荐指数
1
解决办法
1万
查看次数

C#取一个字符串的子串

我只是从文件中取一行,如果该文件没有特定的模式..我想从该行获取最后3个字符...我的代码是:

        while (!line.Contains(pattern))
        {
             String num = line.Substring((line.Length - 3), (line.Length - 2));
             System.Console.WriteLine(num);
        }
Run Code Online (Sandbox Code Playgroud)

但我收到一个错误..

索引和长度必须指向字符串中的位置.参数名称:长度

为什么我这样做?我在行结束前开始新的字符串3个字符,然后我停止2个字符..:\

c# string substring

3
推荐指数
1
解决办法
2040
查看次数

c#获取HashSet的特定元素

我只想在它包含特定字符串的情况下获取HashSet的元素.我尝试了下面的代码,但我没有得到任何东西......就像没有匹配.但是这种情况不会发生,因为UnKnown计数器始终为0.

if (!IsbnAuth.Contains(RecTitle))
{
    Unknown++;
}
else
{
    for (int i = 0; i < IsbnAuth.Count(); i++)
    {
        if (IsbnAuth.ElementAt(i).Contains(RecTitle))
        {
            System.Console.WriteLine(IsbnAuth.ElementAt(i));
            //isbn = IsbnAuth.ElementAt(i).Substring(0, IsbnAuth.ElementAt(i).IndexOf("\t"));
            isbn = IsbnAuth.ElementAt(i).Split(' ')[0];
            break;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?问题不在RecTitle原因,即使它只是一个char,结果也是一样的.IsbnAuth是HashSet.

编辑:IsbnAuth声明

    HashSet<String> IsbnAuth = new HashSet<String>();
    foreach (String line in IsbnAuthors)
    {
        IsbnAuth.Add(line.Trim());
    }
    System.Console.WriteLine(IsbnAuth.Count);
Run Code Online (Sandbox Code Playgroud)

c# hashset

2
推荐指数
1
解决办法
1万
查看次数

用于从字符串接收整数的C#模式创建

我有一些像下面的字符串:

hu212 text = 1
reference = 1
racial construction = 1
2007 = 1
20th century history = 2
Run Code Online (Sandbox Code Playgroud)

我想在'='之后只取整数...我怎么能这样做?我在尝试这个:

 Regex exp = new Regex(@"[a-zA-Z]*[0-9]*[=][0-9]+",RegexOptions.IgnoreCase);
            try
            {
                MatchCollection MatchList = exp.Matches(line);
                Match FirstMatch = MatchList[0];
                Console.WriteLine(FirstMatch.Value);
            }catch(ArgumentOutOfRangeException ex)
            {
                System.Console.WriteLine("ERROR");
            }
Run Code Online (Sandbox Code Playgroud)

但它不起作用......我尝试了其他一些但我得到的结果如"20"或"hu212"......什么exaclty匹配呢?给我剩下的与reg不匹配的字符串?

c# pattern-matching

1
推荐指数
1
解决办法
491
查看次数

带有制表符的java模式

我有一个文件,其中包含以下行:

string1 (tab) sting2 (tab) string3 (tab) string4
Run Code Online (Sandbox Code Playgroud)

我想从每一行得到string3 ...我现在从行开始就是string3在第二个和第三个制表符之间.是否可以采用类似的模式

Pattern pat = Pattern.compile(".\t.\t.\t.");
Run Code Online (Sandbox Code Playgroud)

java tabs pattern-matching

1
推荐指数
2
解决办法
8787
查看次数

C#逐行检查文件

我正在逐行读取文件,我想编辑我读过的一些行.我想编辑的行必须在它们上面或下面有一些其他特定行,但我不知道如何在C#中表达.例如:

http://www.youtube.com
You Tube | Music | something
Sports
Music
Radio
Clips
http://www.youtube.com/EDIT ME   
Sports
Music
Radio
Clips
Run Code Online (Sandbox Code Playgroud)

我想只在下一行编辑一行

Sports 
Run Code Online (Sandbox Code Playgroud)

而上一行是

Clips
Run Code Online (Sandbox Code Playgroud)

所以我想从上面的例子中编辑的唯一一行是

http://www.youtube.com/EDIT ME 
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

c# file-io line-by-line

0
推荐指数
1
解决办法
378
查看次数