明輝手游網(wǎng)中心:是一個(gè)免費(fèi)提供流行視頻軟件教程、在線學(xué)習(xí)分享的學(xué)習(xí)平臺(tái)!

一些對(duì)于VB中字符串設(shè)置的問題與回答

[摘要]提問:假設(shè)我從Excel表格中復(fù)制了一些數(shù)據(jù)到剪貼板中,比如是這樣一些信息:Allen 12Anderson 13Douglas 12Ohio 49我怎樣才能把這些名字和數(shù)字讀進(jìn)一個(gè)數(shù)組或者一個(gè)Grid框中呢?用Clipboard.GetText(vbCFText)只能一下子讀入所有數(shù)據(jù),而我希望...
提問:
假設(shè)我從Excel表格中復(fù)制了一些數(shù)據(jù)到剪貼板中,比如是這樣一些信息:
Allen 12
Anderson 13
Douglas 12
Ohio 49

我怎樣才能把這些名字和數(shù)字讀進(jìn)一個(gè)數(shù)組或者一個(gè)Grid框中呢?用Clipboard.GetText(vbCFText)只能一下子讀入所有數(shù)據(jù),而我希望一個(gè)一個(gè)地讀。

回答:
新建一個(gè)項(xiàng)目,在窗體上放兩個(gè)label和一個(gè)command。以下是代碼:
Private Sub Command1_Click()
  Dim vTekst$
  
  vTekst$ = "Allen 12 "
  vTekst$ = vTekst$ & "Anderson 13 "
  vTekst$ = vTekst$ & "Bernard 14 "
  vTekst$ = vTekst$ & "Constance 15 "
  Label1.Caption = vTekst$
  
  Select Case Command1.Caption
  Case "Copy Clipboard"
    Clipboard.Clear
    Clipboard.SetText Label1.Caption
    Command1.Caption = "Put into Label"
  Case "Put into Label"
    Label2.Caption = GetPartofString(Clipboard.GetText, 1)
    Command1.Caption = "Copy Clipboard"

    'read in array
    Dim vText(7) As String
    Dim c%
  
    For c% = 0 To 7
      vText(c%) = GetPartofString(Clipboard.GetText, c% + 1)
    Next c%
    
    'show result
    For c% = 0 To 7
      MsgBox vText(c%)
    Next c%

End Sub

Private Function GetPartofString(source$, part%) As String
  Dim p%, c%, tmp$
  
  tmp$ = source$
  c% = 0
  Do
    p% = InStr(tmp, Chr(32))
    If p% <> 0 Then
      GetPartofString = Left(tmp, p% - 1)
      c% = c% + 1
      tmp = Right(tmp, Len(tmp) - p%)
    End If
  Loop While c% <> part%
  
End Function
--1-------------------------------------------------------------

提問:
我如何才能數(shù)出一個(gè)字符串中的字母數(shù)?舉例來說:我想在用戶按下OK時(shí)計(jì)算在Text1.Text中的字母數(shù)。

回答:
使用LEN(Text1.text)命令如何?你就可以得到長(zhǎng)度……包括空格和其它非字母的字符。所以如果你希望去掉它們的話,必須要一個(gè)小函數(shù)來檢查Text1.Text中的字符是否為真正的字母:

Public Function CountCharacters(source$) As Integer
  Dim counter%, t%
  Const Characters$ = "abcdefghijklmnopqrstuvwxyz"

  For t% = 1 To Len(source$)
    If InStr(Characters, LCase$(Mid$(source$, t%, 1))) <> 0 Then
      counter% = counter% + 1
    End If
  Next t%
  CountCharacters = counter%
End Function

使用時(shí)就象這樣:
vString$ = "Testing .... about what?"
MsgBox CountCharacters(vString$)

--2-------------------------------------------------------------

提問:
有沒有人知道怎樣來做這樣一個(gè)特殊的循環(huán)?我需要把一個(gè)字符串,比如“Hey how are you”,中的每個(gè)字母放到不同的變量中。例如對(duì)上面這句話,H放在A1中,e放在A2中,以此類推。謝謝你的幫助。

回答:
Dim vChar() as String

Sub PlaceInArray(source$)

Dim p%

For p% = 1 To Len(source$)
Redim Preserve vChar(p%)
vChar(p%) = Mid$(source$,p%,1)
Next p%
End Sub
在數(shù)組vChar的每一項(xiàng)中就分別是給出字符串的所有字母。

--3-------------------------------------------------------------

提問:
你怎樣把一個(gè)文本文件中的單詞一個(gè)一個(gè)讀入字符串變量中?文件中每個(gè)單詞都被空格分隔。

回答:
Dim vWords() as String

Sub SplitStringintoWords(bron$)
Dim c%, p%, t%, vCheck%
Dim TempBron$, tmp$

'把一行輸入分成單詞
  t% = 0
  TempBron$ = bron$
  For c% = 1 To Len(bron$)
    p% = InStr(TempBron$, Chr(32))
    If p% <> 0 Then
    ReDim Preserve vWords(t%)
      tmp = Left$(TempBron$, p% - 1)
      vWords(t%) = StripString(tmp)
      TempBron$ = Right$(TempBron$, Len(TempBron$) - p)
      t% = t% + 1
      c% = c% + p%
    End If
  Next c%
  ReDim Preserve vWords(t%)
  vWords(t%) = StripString(TempBron)

End Sub

首先你必須先讀入一行文本,然后使用以上這個(gè)過程。在數(shù)組vWords中就是文本文件中的所有單詞。如果你在讀文件方面也需要幫助,告訴我。
--4-------------------------------------------------------------

提問:
我需要替換窗體上所有文本框中的某一個(gè)單詞,應(yīng)該怎么做?先說聲謝謝了。

回答:
試試這個(gè)……

'在新窗體上放一個(gè)command和三個(gè)textbox,然后加入以下代碼
'按F5運(yùn)行,注意先把Command的caption改成"&Replace somebody with me"

Private Sub Command1_Click()
  Const vString$ = "testing for somebody on the net"
  Select Case Command1.Caption
  Case "&Replace text in all textboxes"
    Text1.Text = vString
    Text2.Text = vString
    Text3.Text = vString
    Command1.Caption = "&Replace somebody with me"
  Case "&Replace somebody with me"
    Call ChangeText
    Command1.Caption = "&Replace text in all textboxes"
  End Select
  
End Sub

Function sReplace(SearchLine As String, SearchFor As String, ReplaceWith As String)
  Dim vSearchLine As String, found As Integer
  
  found = InStr(SearchLine, SearchFor): vSearchLine = SearchLine
  If found <> 0 Then
    vSearchLine = ""
    If found > 1 Then vSearchLine = Left(SearchLine, found - 1)
    vSearchLine = vSearchLine + ReplaceWith
    If found + Len(SearchFor) - 1 < Len(SearchLine) Then _
      vSearchLine = vSearchLine + Right$(SearchLine, _
  Len(SearchLine) - found - Len(SearchFor) + 1)
  End If
  sReplace = vSearchLine
  
End Function

Private Sub ChangeText()
  Dim Control

  For Each Control In Form1.Controls
    If TypeOf Control Is TextBox Then
      Control.Text = sReplace(Control.Text, "somebody", "me")
    End If
  Next Control
    
End Sub
--5-------------------------------------------------------------

提問:
現(xiàn)在我有一個(gè)文本框來輸入字符串。另外有60個(gè)文本框的控件數(shù)組,它們包含了我的一個(gè)字典。我設(shè)法檢查字符串中的每一個(gè)單詞,看它是否符合控件數(shù)組中的也就是字典中的單詞。我在檢驗(yàn)前不得不把標(biāo)點(diǎn)全部去掉。如果整個(gè)句子都拼寫對(duì)了,我發(fā)出一個(gè)消息,如果拼錯(cuò)了,發(fā)出另一個(gè)消息。
感謝所有對(duì)此提出的任何建議和想法。

回答:
假定你有一個(gè)文本框來輸入單詞,然后你對(duì)照一個(gè)數(shù)組來檢查它們……你可以使用以下代碼作為你的起點(diǎn)……但是請(qǐng)考慮把你的字典轉(zhuǎn)換成一個(gè)數(shù)據(jù)庫(kù),因?yàn)楫?dāng)它成為一個(gè)真正的字典時(shí),數(shù)據(jù)庫(kù)工作起來快得多。

'on the general section
Dim vWords()
Dim Max%
Dim vCheckWord()

Private Form1_Load()
  Text1.SetFocus
  Text1.Text = "living in america but not really"
  Max% = 10
  
  'make array
  'you can use an ascii file to get the words
  Redim vCheckWord(Max)
  vCheckWord(0) = "walther"
  vCheckWord(1) = "musch"
  vCheckWord(2) = "america"
  vCheckWord(3) = "tilburg"
  vCheckWord(4) = "hallo"
  vCheckWord(5) = "testen"
  vCheckWord(6) = "testing"
  vCheckWord(7) = "really"
  vCheckWord(8) = "visual"
  vCheckWord(9) = "basic"

End Sub

Sub SplitStringintoWords(bron$)
Dim c%, p%, t%, vCheck%
Dim TempBron$, tmp$
Dim vOke As Boolean

'splitting the input into words
  t% = 0
  TempBron$ = bron$
  For c% = 1 To Len(bron$)
    p% = InStr(TempBron$, Chr(32))
    If p% <> 0 Then
    ReDim Preserve vWords(t%)
      tmp = Left$(TempBron$, p% - 1)
      vWords(t%) = StripString(tmp)
      TempBron$ = Right$(TempBron$, Len(TempBron$) - p)
      t% = t% + 1
      c% = c% + p%
    End If
  Next c%
  ReDim Preserve vWords(t%)
  vWords(t%) = StripString(TempBron)

'checking against spellingschecker
  vOke = False
  For c% = 0 To t%
    For vCheck% = 0 To Max
      If vCheckWord(vCheck%) <> vWords(c%) Then
        vOke = False
      Else
        vOke = True
        vCheck% = Max%
      End If
    Next vCheck%
    If Not vOke Then MsgBox vWords(c%)
    vOke = False
  Next c%

End Sub


Private Sub Text1_KeyPress(KeyAscii As Integer)
  If KeyAscii = 13 Then
    'split string into words
    Call SplitStringintoWords(Text1.Text)
  End If
    
End Sub

Function StripString(source As String) As String
Const Letters$ = "abcdefghijklmnopqrstuvwxyz"
Dim p%, tmp$

  tmp = source$
  For p% = 1 To Len(source$)
    If InStr(Letters, LCase(Mid$(source$, p%, 1))) = 0 Then
      Select Case p%
      Case 1
        tmp = Right$(source$, Len(source$) - p%)
      Case Len(source$)
        tmp = Left$(source$, Len(source$) - 1)
      Case Else
        tmp = Left$(source$, p%) & Right$(source$, Len(source$) - p%)
      End Select
    End If
  Next p%
  StripString = tmp
      
End Function

--6-------------------------------------------------------------

提問:
我需要幫助,如何判斷一個(gè)文件名是否有后綴名,然后剝除這個(gè)后綴。原本我是想獲得文件名,然后用另一個(gè)后綴名來把它存為一個(gè)備份文件。有沒有一個(gè)方便的方法,不用搜索整個(gè)路徑字符串中的“\”。

回答:
從路徑字符串的后面向前查找第一個(gè)“\”,用Right$命令,這樣你就可以得到文件名。
如果你要?jiǎng)h掉后綴名,也這么做,只是查找第一個(gè)“.”而已。注意在W95中,文件名可以包括一個(gè)以上的“.”

可以使用這樣的代碼:
Dim p%

Function GetFileName(PathString$) As String

GetFileName = PathString
For p% = Len(PathString) To 0 Step -1
If Mid$(PathString,p%,1) = "\" then
GetFileName = Right$(PathString,p%)
Exit Function
End If
Next p%
End Function
--7-------------------------------------------------------------