2

我想要一个能够写入文件的程序:

addr_new = serverInfo.REGION_DICT[0][serverNum]["channel"][serverChannel]["ip"]   

, 但必须忽略文本中的字符“”

我的结果:Visual Studio 不会忽略字符串中的字符“”,而是需要 IP 等字符串。谢谢帮助

对不起我的英语

有我的程序:

 string path = System.IO.Directory.GetCurrentDirectory()+ "logininfo.py";

    string[] lines = {
    "import serverInfo"
    , "serverNum=1"
    , "serverChannel=1"
    , "addr_new = serverInfo.REGION_DICT[0][serverNum]["channel"][serverChannel]["ip"]"};

System.IO.StreamWriter file = new System.IO.StreamWriter(path);
file.WriteLine(lines);

file.Close();

我差点忘了我想要更多的行,这个程序被修剪了

4

3 回答 3

4

You can escape those characters with a slash:

string[] lines = {
    " import serverInfo" ,
    "serverNum=1" ,
    "serverChannel=1" ,
    "addr_new = serverInfo.REGION_DICT[0][serverNum][\"channel\"][serverChannel][\"ip\"] "};

or use a @-quoted string and use "" instead of " like so:

string[] lines = {
    " import serverInfo" ,
    "serverNum=1" ,
    "serverChannel=1" ,
    @"addr_new = serverInfo.REGION_DICT[0][serverNum][""channel""][serverChannel][""ip""] "};
于 2012-11-24T12:48:42.900 回答
2

You need to concatenate the strings:

"addr_new = serverInfo.REGION_DICT[0][serverNum][" + channel + "][serverChannel][" + ip + "]"};

Or if you wish for "channel" to be used as a string value, use single quotes:

"addr_new = serverInfo.REGION_DICT[0][serverNum]['channel'][serverChannel]['ip']"};
于 2012-11-24T12:47:06.443 回答
0

Escape the special characters

"addr_new = serverInfo.REGION_DICT[0][serverNum][\"channel\"][serverChannel][\"ip\"]"
于 2012-11-24T12:49:54.877 回答