vb.net 读取txt 并替换指定字符串问题

Dim fs As New FileStream("D:尀2.txt", FileMode.Open) Dim sr As New StreamReader(fs) Dim str As String Dim temp As String str = sr.ReadToEnd.ToString MsgBox(str) sr.Close() fs.Close() temp = str temp.Replace("111", "22") MsgBox(temp) 原来的txt文件里的内容是111,现在想从txt文件中读取内容,保存在一个字符串变量中,并替换。如今的问题是能读出文件内容,但是无法替换,请求原因!
2026年09月28日 14:22
有1个网友回答
网友(1):

语法错误,没有存储函数的返回值。
temp.Replace("111", "22")
这个函数方法返回修改后的结果,并不修改参数变量本身,也就是按值传递,而不是按地址传递,正确用法:
temp=temp.Replace("111", "22")