2012-11-22 66 views
-1

我想要在引號中顯示文本並顯示它。獲取文本並將其替換爲vb.net

名爲 「約翰」

和什麼是在文本框中

名 「不管」

這是所有.txt文件

+2

只是看它:1。如何從一個文本框2.如何寫的文字在我的愚見文件這是不是真的值得SO問題得到文本發現,這不是一個非常具體的問題,只是谷歌它。 – Aaron

+2

使用[String.Replace](http://msdn.microsoft.com/en-us/library/system.string.replace.aspx)。 – Neolisk

+0

我做過谷歌它。 –

回答

0

嘗試使用Google VB更換。 net + streamreader和vb.net + streamwriter的例子,那應該會給你想要的。

+0

我知道如何閱讀一個文件..但我不知道如何在引號之後如果找到單詞名稱。 –

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 
+0

我想通了。謝謝! –

相關問題