正文

关于字符串的操作2005-10-11 20:24:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/iamben250/5919.html

分享到:

  转发---关于字符串的操作 提问:假设我从Excel表格中复制了一些数据到剪贴板中,比如是这样一些信息:Allen 12Anderson 13Douglas 12Ohio 49我怎样才能把这些名字和数字读进一个数组或者一个Grid框中呢?用Clipboard.GetText(vbCFText)只能一下子读入所有数据,而我希望一个一个地读。回答:新建一个项目,在窗体上放两个label和一个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 SubPrivate 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-------------------------------------------------------------提问:我如何才能数出一个字符串中的字母数?举例来说:我想在用户按下OK时计算在Text1.Text中的字母数。回答:使用LEN(Text1.text)命令如何?你就可以得到长度……包括空格和其它非字母的字符。所以如果你希望去掉它们的话,必须要一个小函数来检查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使用时就象这样:vString$ = "Testing .... about what?"MsgBox CountCharacters(vString$)--2-------------------------------------------------------------提问:有没有人知道怎样来做这样一个特殊的循环?我需要把一个字符串,比如“Hey how are you”,中的每个字母放到不同的变量中。例如对上面这句话,H放在A1中,e放在A2中,以此类推。谢谢你的帮助。回答:Dim vChar() as StringSub PlaceInArray(source$)Dim p%For p% = 1 To Len(source$)Redim Preserve vChar(p%)vChar(p%) = Mid$(source$,p%,1)Next p%End Sub在数组vChar的每一项中就分别是给出字符串的所有字母。--3-------------------------------------------------------------提问:你怎样把一个文本文件中的单词一个一个读入字符串变量中?文件中每个单词都被空格分隔。回答:Dim vWords() as StringSub 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首先你必须先读入一行文本,然后使用以上这个过程。在数组vWords中就是文本文件中的所有单词。如果你在读文件方面也需要帮助,告诉我。--4-------------------------------------------------------------提问:我需要替换窗体上所有文本框中的某一个单词,应该怎么做?先说声谢谢了。回答:试试这个……'在新窗体上放一个command和三个textbox,然后加入以下代码'按F5运行,注意先把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 SubFunction 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 FunctionPrivate 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-------------------------------------------------------------提问:现在我有一个文本框来输入字符串。另外有60个文本框的控件数组,它们包含了我的一个字典。我设法检查字符串中的每一个单词,看它是否符合控件数组中的也就是字典中的单词。我在检验前不得不把标点全部去掉。如果整个句子都拼写对了,我发出一个消息,如果拼错了,发出另一个消息。感谢所有对此提出的任何建议和想法。回答:假定你有一个文本框来输入单词,然后你对照一个数组来检查它们……你可以使用以下代码作为你的起点……但是请考虑把你的字典转换成一个数据库,因为当它成为一个真正的字典时,数据库工作起来快得多。'on the general sectionDim 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 SubSub 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 SubPrivate Sub Text1_KeyPress(KeyAscii As Integer)  If KeyAscii = 13 Then    'split string into words    Call SplitStringintoWords(Text1.Text)  End If    End SubFunction StripString(source As String) As StringConst 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-------------------------------------------------------------提问:我需要帮助,如何判断一个文件名是否有后缀名,然后剥除这个后缀。原本我是想获得文件名,然后用另一个后缀名来把它存为一个备份文件。有没有一个方便的方法,不用搜索整个路径字符串中的“\”。回答:从路径字符串的后面向前查找第一个“\”,用Right$命令,这样你就可以得到文件名。如果你要删掉后缀名,也这么做,只是查找第一个“.”而已。注意在W95中,文件名可以包括一个以上的“.”可以使用这样的代码:Dim p%Function GetFileName(PathString$) As StringGetFileName = PathStringFor p% = Len(PathString) To 0 Step -1If Mid$(PathString,p%,1) = "\" then GetFileName = Right$(PathString,p%)Exit FunctionEnd IfNext p%End Function

阅读(3739) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册