我有一个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) 我只是从文件中取一行,如果该文件没有特定的模式..我想从该行获取最后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个字符..:\
我只想在它包含特定字符串的情况下获取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) 我有一些像下面的字符串:
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不匹配的字符串?
我有一个文件,其中包含以下行:
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) 我正在逐行读取文件,我想编辑我读过的一些行.我想编辑的行必须在它们上面或下面有一些其他特定行,但我不知道如何在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# ×5
dictionary ×1
file-io ×1
hashset ×1
java ×1
line-by-line ×1
string ×1
substring ×1
tabs ×1