删除所有与QUOT;无形"从字符串中的字符?字符串、字符、QUOT

2023-09-04 11:55:30 作者:输给感情

我正在写一个小类从文件中读取的键值对的列表,并写入词典<字符串,字符串> 。该文件将具有以下格式:

I'm writing a little class to read a list of key value pairs from a file and write to a Dictionary<string, string>. This file will have this format:

key1:value1
key2:value2
key3:value3
...

这应该是pretty的容易做到,但由于用户会手动编辑这个文件,我应该怎么处理空格,制表符,多余的跳跃之类的东西?我大概可以使用替换以删除空格和标签,但是,没有任何其他的看不见的角色我失踪?

This should be pretty easy to do, but since a user is going to edit this file manually, how should I deal with whitespaces, tabs, extra line jumps and stuff like that? I can probably use Replace to remove whitespaces and tabs, but, is there any other "invisible" characters I'm missing?

或者,也许我可以删除非字母数字,所有字符:和一行跳转(因为行跳跃是什么分离一对来自其他),并删除所有多余的跳跃。如果这个,我不知道如何删除全除,一些字。

Or maybe I can remove all characters that are not alphanumeric, ":" and line jumps (since line jumps are what separate one pair from another), and then remove all extra line jumps. If this, I don't know how to remove "all-except-some" characters.

当然,我也可以检查类似的错误键1:值1:somethingelse。但是,这样的东西并没有真正的问题多,因为它显然对用户的错,我也只是显示无效格式的消息。我只是想应付基本的东西,然后把所有的在一个try / catch块,以防万一别的出错。

Of course I can also check for errors like "key1:value1:somethingelse". But stuff like that doesn't really matter much because it's obviously the user's fault and I would just show a "Invalid format" message. I just want to deal with the basic stuff and then put all that in a try/catch block just in case anything else goes wrong.

注意:我不需要任何空格可言,甚至在一个键或值

Note: I do NOT need any whitespaces at all, even inside a key or a value.

推荐答案

的要求过于模糊。试想一下:

The requirements are too fuzzy. Consider:

当是一个空间的值?钥匙? 如果是分隔符值?钥匙? 当一个标签值?钥匙? 哪里,当一个分隔符是用在一个值的情况下的数值到底?键?

"When is a space a value? key?" "When is a delimiter a value? key?" "When is a tab a value? key?" "Where does a value end when a delimiter is used in the context of a value? key"?

这些问题将导致code充斥着一次性的和较差的用户体验。这就是为什么我们有语言规则/语法。

These problems will result in code filled with one off's and a poor user experience. This is why we have language rules/grammar.

定义了一个简单的语法,并采取了大部分的猜测。

Define a simple grammar and take out most of the guesswork.

{}键{}值,

在这里,你必须包含引号内,并通过分隔符分隔的键/值对(,)。所有多余的字符可以被忽略。你可以使用使用XML,但是这可能会吓跑少易怒的用户。

Here you have a key/value pair contained within quotes and separated via a delimiter (,). All extraneous characters can be ignored. You could use use XML, but this may scare off less techy users.

请注意,引号是任意的。随意更换,不会需要太多逃脱(只是要小心的复杂性),任何一组的容器。

Note, the quotes are arbitrary. Feel free to replace with any set container that will not need much escaping (just beware the complexity).

就个人而言,我想这包装在一个简单的用户界面和序列化数据输出为XML。有次不这样做,但你给我没有理由不去。

Personally, I would wrap this up in a simple UI and serialize the data out as XML. There are times not to do this, but you have given me no reason not to.