-1

我想获取引号中的文本并显示它。

名字“约翰”

并将其替换为文本框中的内容

取名为“随便”

这都在一个 .txt 文件中

4

2 回答 2

1

您必须逐行阅读文件,逐字拆分并找到双引号文本,将其替换为文本框文本。

我已经给出了阅读和替换的代码。最后做一个写函数,把它写回一个txt文件。

用法:

Try
     ' Create an instance of StreamReader to read from a file.
     Dim sr As StreamReader = New StreamReader("TestFile.txt")
     Dim line As String
     Dim FullContent as string=""
     ' Read and display the lines from the file until the end 
     ' of the file is reached.

     Dim strArray() as string=Nothing

Do
   Array.Clear(strArray,0,strArray.Length)
   line = sr.ReadLine()

   strArray=line.split(" ")     'To read Every Word Seperated by a Space
   For i=0 to strArray.Count-1

       'Checks StartsWith and EndsWith "       - Eg. Ashi likes "firefox"
       If strArray(i).StartsWith("""") and strArray(i).EndsWith("""") then

          'Replace the string in "firefox" with TextBox1 text 
          line.Replace("""" & strArray(i) & """",trim(TextBox1.Text))

       End If
   FullContent = FullContent & line      'Append the changes to full content
   Next


 Loop Until line Is Nothing
   sr.Close()

   'Now you can write the FullContent Back to Txt File which has the replaced changes
   'writeFunction(FullContent)

   'If you want to display, then
   msgbox(FullContent)


Catch E As Exception
     ' Let the user know what went wrong.
     Console.WriteLine("The file could not be read:")
     Console.WriteLine(E.Message)
End Try
于 2012-11-23T01:51:18.340 回答
0

尝试谷歌搜索 vb.net + streamreader 和 vb.net + streamwriter 示例,这应该可以满足您的需求。

于 2012-11-22T03:03:42.037 回答