忽略字符串中的字符“”

时间:2012-11-24 12:43:22

标签: c# string special-characters

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

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();

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

3 个答案:

答案 0 :(得分:4)

您可以使用斜杠转义这些字符:

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

或使用@-quoted string并使用""代替",如下所示:

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

答案 1 :(得分:2)

您需要concatenate字符串:

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

或者,如果您希望将“channel”用作字符串值,请使用单引号:

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

答案 2 :(得分:0)

转义特殊字符

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