简体   繁体   English

在C#中从字符串中删除行+从字典中删除旧条目

[英]Removing lines from a string + old entries from a Dictionary in C#

I have a Dictionary the first string, the key's, must never change.. it cant be deleted or anything.. but the value, i keep adding lines, and lines, and lines to the values.. i just create new lines with \\r\\n or \\r .. and im just wondering what would be the easiest way to retain just the last 50 lines. 我有一个字典,第一个字符串,键的键,永远都不能改变..它不能被删除或任何其他..但是值,我一直在添加行,行和行到值..我只是用\\创建新行r \\ n或\\ r ..我只是想知道保留最后50行是最简单的方法。 and delete anything over the 50 lines.. im doing this because when i return it i have to put the values through a char array, and go through each letter, and this can be slow if there is too much data. 并删除50行以上的任何内容..我这样做是因为当我返回它时,我必须将值通过char数组放置,并遍历每个字母,如果数据太多,这可能会很慢。 any suggestions? 有什么建议么?

Guffa's general idea is right - your data structure should reflect what you actually want, which is a list of strings rather than a single string. Guffa的一般想法是正确的-您的数据结构应反映您实际想要的内容,它是字符串列表,而不是单个字符串。 The concept of "the last 50 lines" is pretty obviously to do with a collection rather than a single string, even if you've originally read it that way. “最后50行”的概念很明显与集合有关,而不是与单个字符串有关,即使您最初是这样阅读的。

However, I'd suggest using a LinkedList<T> rather than a List<T> : every time you remove the first element of a List<T> , everything else has to shuffle up. 但是,我建议使用LinkedList<T>而不是List<T> :每次删除List<T>的第一个元素时,其他所有内容都必须重新整理。 List<T> is great for giving random access and not too bad at adding to the end, but sucks for removing from the start. List<T>对于提供随机访问非常有用,并且在添加到末尾时也不错,但是从一开始就删除很烂。 LinkedList<T> is great at giving you iterator access, adding to / removing from the start, and adding to / removing from the end. LinkedList<T>非常适合为您提供迭代器访问权限,从一开始就添加/删除,并从最后添加/删除。 It's a better fit. 比较合适。 (If you really wanted to go to town you could even write your own fixed-size circular buffer type which encapsulated the logic for you; this would give the best of both worlds, in the situation where you don't want to be able to expand beyond a certain size.) (如果您真的想去城镇,您甚至可以编写自己的固定大小的循环缓冲区类型来封装您的逻辑;在您不希望出现这种情况的情况下,这样做可以兼得两全扩展到一定大小。)

Regarding your comments to Guffa's answer: it's pretty common to convert input into a form which is more appropriate for processing, then convert it back to the original format for output. 关于您对Guffa答案的评论:将输入转换为更适合处理的形式,然后将其转换回原始格式以进行输出是很常见的。 The reason why you do it is precisely the "more appropriate" bit. 您这样做的原因恰好是“更合适的”位。 You don't want to have to parse the string for line breaks as part of the "updating the dictionary" action, IMO. IMO不需要将字符串解析为换行符,这是“更新字典”操作的一部分。 In particular, it sounds like you're currently introducing the idea of "lines" where the original text is just being read in as strings. 特别是,听起来您当前正在引入 “行”的概念,其中原始文本只是作为字符串读取。 You're effectively creating your own "collection" class backed by a string, by delimiting strings with line breaks. 通过用换行符分隔字符串,可以有效地创建由字符串支持的自己的“集合”类。 That's inefficient, error-prone, and much harder to manage than using the built-in collections. 与使用内置集合相比,这种方法效率低下,容易出错,并且更难管理。 It's easy to perform the conversion to a line-break-delimited string at the end if you want it, but it sounds like you're doing it way too early. 如果需要,可以很容易地在结尾处执行以换行符分隔的字符串的转换,但这听起来太早了。

Instead of concatenating the lines, use a Dictionary<string, List<string>> . 而不是连接线,而是使用Dictionary<string, List<string>> When you are about to add a string to the list you can check the count and remove the first string if the list already has 50 strings: 当您要向列表中添加字符串时,如果列表中已经包含50个字符串,则可以检查计数并删除第一个字符串:

List<string> list;
if (!theDictionary.TryGetValue(key, out list)) {
  theDictionary.Add(list = new List<string>());
}
if (list.Count == 50) {
  list.RemoveAt(0);
}
list.Add(line);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM