📚 知識家圖書館 奇摩知識+ 紀念館

VB在一篇文章那找字

發問者 *邱a~看未來 (Lv.1) ・2008 年 6 月 24 日 ・程式設計

老師要我們在一篇文章中找字。 例如:This is a novel that is very interesting. If I have enough money, I would buy it. An animatronic system including lifelike robotic fish. This dog is very tiny. 當你要找尋 is 時就會出現有is的單字。 像是會This is is fish This is全部出現。 可以幫我打出跟下列不一樣的程式碼嗎? Private Sub Form_Activate() Dim read As String Dim j As Integer Dim searchCh As String Dim token Open App.Path & "\" + "a.txt" For Input As #1 Open App.Path & "\" + "b.txt" For Output As #2 searchCh = InputBox("輸入搜索字元") Do Until EOF(1) Input #1, read token = Split(read, " ") j = 0 While j < UBound(token) + 1 If InStr(token(j), searchCh) <> 0 Then Write #2, token(j) & " " Print token(j) End If j = j + 1 Wend Loop Close #1 Close #2 End Sub

1 則回答

最佳解答
Private Sub Command1_Click() '執行的按鈕 Call Form_Activate End Sub Private Sub Form_Activate() s = InputBox("輸入搜索字元"): If s = "" Then Exit Sub'沒輸入就不動作 Open App.Path & "\" + "a.txt" For Input As #1 Open App.Path & "\" + "b.txt" For Output As #2 Do Until EOF(1) '* Line Input #1, temp '讀入一整行 If Trim(temp) <> "" Then '不是空白行才需要搜索 ** temp = Replace(temp, ".", " ") '把句點取代為空白 temp = Replace(temp, ",", " ") '把逗點取代為空白 s1 = Split(temp, " ") '分解為單字陣列 For i = 0 To UBound(s1) '利用迴圈跑過每一個字 *** If InStr(1, UCase(s1(i)), UCase(s)) Then Print #2, s1(i) & " "; '用ucase避免大小寫不同 Next i '*** End If '** Loop '* Close '關閉所有開啟的檔案 MsgBox "ok!" End Sub 2008-06-24 12:22:54 補充: '要在form1裏也出現: 這行: If InStr(1, UCase(s1(i)), UCase(s)) Then Print #2, s1(i) & " "; '用ucase避免大小寫不同 改成: If InStr(1, UCase(s1(i)), UCase(s)) Then Print #2, s1(i) & " "; '輸出到檔案裏 Print s1(i) & " "; '輸出到表單上 end if 2008-06-24 22:41:29 補充: 問題出在你的這行: Print s1(i) & " " 和阿戊補充的不一樣...它是在 IF...end if 裏的...而不是獨立在外面 不然你就要串在上一行的後面,才有同樣的結果: If InStr(1, UCase(s1(i)), UCase(s)) Then Print #2, s1(i) & " " : Print s1(i) & " "
阿戊(Lv.7) 2008 年 6 月 24 日

電腦與網際網路的其他問題