C# reading a tab character as a setting from text file

强颜欢笑 提交于 2019-12-11 19:19:28

问题


Good Day, i joined today.

I have a setting in text-file.

logSeparator=\t

this will be read into Properties.Settings(string) during start-up.

when I read this into a string it will become "\\t"

so I have a temporary fix to keep things working:

if (Properties.Settings.Default.logSeparator == "\\t") { //trim \t

            Properties.Settings.Default.logSeparator = "\t";
            Properties.Settings.Default.Save();

        }

there should be some easy way to clean this string and make it as a char

"\t", now, if enduser decides to start using for example "\r" as separator

character it wouldn't work.

I am looking something easy like this: string_name.Replace("\\", "\"); //(this does not work)

please help, I am sure there has to be easy and clean way of doing this, however spent quite a time with google trying to figure this out and couldn't find anything.


回答1:


If you just use string.Format(logSeparator) it will convert it. You don't need to do your \ stripping. I'm guessing what you are seeing in the watch window will be the escaped version of the character sequence. i.e. the string \t will show in the watch window as \\t to differentiate it from a tab character - it doesn't actually have two backslashes in the string.




回答2:


Your problem is in how you perceive these being translated into tabs. In your file, it's just ordinary characters: \t. But the actual tab character is different. When C# processes literal strings, it converts \t into a tab for you (unless you tell it not to with the @ string prefix).

So what you need to do is emulate the substitution. You already tried this, but targeted the wrong character. What you need to do is this:

string_name.Replace("\\t", "\t")

There are better ways to substitute control characters, and you might want to consider allowing an escaped backslash. But at the very basic level, this should get you going.




回答3:


Since the backslash is escaped by C# automatically the last character doesn't include the backslash.

I would suggest changing the type of logSeparator to char, since this behavior will occur any time you use the string. Here's a simple method that takes a string and will return the correct control character according to the last character in the string:

    private char GetSeparator(string Input)
    {
        char separator = new char();
        switch (Input.Last())
        {
            case 't':
                separator = '\t';
                break;
            case 'r':
                separator = '\r';
                break;

        }
        return separator;
    }

You can easily add any other case statements that will be required. I assumed you're using the readline method so that the new line character is trimmed. If not that may need to be done first.



来源:https://stackoverflow.com/questions/17396450/c-sharp-reading-a-tab-character-as-a-setting-from-text-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!