2012-11-24 184 views
2

我想有一個程序,就可以寫在文件中的字符串:忽略字符「」

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

,但必須忽略的人物「」文本

我restult: Visual Studio中沒有字符串忽略characters「」,並希望像IP等字符串.. THX的幫助

Sry基因,我的英語

還有就是我的計劃:

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

可以逃避這些字符以斜線:

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""] "}; 
2

您需要concatenate字符串:

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

或者,如果你願意的「通道」,可以作爲一個字符串值,使用單引號:

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

我不希望使用的信道等。我想寫入文件以下行: addr_new = serverInfo.REGION_DICT [0] [serverNum] [「channel」] [serverChannel] [「ip」] – user1849432

+0

@ user1849432我無法正確理解問題。我已經更新了我的答案,我相信你或者需要轉義角色,或者使用單引號,以免混淆腳本。 – Curt

0

逃離特殊字符

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