2011-09-03 29 views
0

我有一個問題涉及字符串Replace函數在Visual Basic .NET中:如何替換VB.NET中的子字符串?

我有我的項目的Visual Basic腳本。我有一個名爲RichTextBoxsample

Dim string1 as string = "text to find" 
Dim string2 as string = "text to replace find with" 
Dim mediacurrent as string 

mediacurrent = sample.text 

mediacurrent.replace(string1, string2) 

sample.text = mediacurrent 

以上腳本返回空白文本框。注意文本框很豐富,包含非格式但多行文本。我究竟做錯了什麼?

回答

7

字符串是在.NET不變,該Replace方法返回新的價值,它不會修改它被稱爲原始字符串。你需要重新分配它,就像這樣:

mediacurrent = mediacurrent.Replace(string1, string2)