2014-11-06 11 views
-2

在C#中我有一個變量:不是普通的字符串替換問題

string text = "some nice text #ttt#"; 

現在我要替換#TTT#與3串的串聯;

string str1 = "\'%"; 
textbox1.Text = "AAA"; 
string str2 = "%\'"; 

所以我做的:

//it returns: "some nice text '%%'" 
string text = String.Replace("#ttt#", String.Concat(str1, textbox1.Text, str2)); 

//it returns: "some nice text '%%'" 
string text = String.Replace("#ttt#", (str1+textbox1.Text+str2)); 

如果我在debuger狙擊String.Concat(str1, textbox1.Text, str2)它顯示正確"some nice text '%AAA%'"

任何想法如何取代它這樣%%之間會textbox1.Text字符串?

+1

我知道正則表達式。它是一個使用它的想法,但它不是我的問題的答案,爲什麼它不能正確替換。 – gemGreg 2014-11-06 01:28:28

+1

'String.Replace(「#ttt#」,...'不應該編譯 - 你能提供實際的樣本嗎? – 2014-11-06 01:28:42

+1

你發佈的內容不會編譯,請顯示你的真實代碼。 – DavidG 2014-11-06 01:28:47

回答

2

你不是替換文本:現在

string text = "some nice text #ttt#"; 
string str1 = "\'%"; 
string xxx = "AAA"; 
string str2 = "%\'"; 
string result = text.Replace("#ttt#", String.Concat(str1, 
xxx, str2)); 

結果是 「一些不錯的文字 '%AAA%'」