VBGood网站全文搜索 Google

首页 - 经验之谈 - 使用VB6的Split函数计算子串
发表评论(0)作者:不详, 平台:VB6.0+Win98, 阅读:18611, 日期:2000-11-08
Use the Visual Basic 6.0 Split function to count substrings

As we mentioned in a previous tip, Visual Basic 6.0 introduced the Split function, which provides a new way to parse strings. With it, you indicate a delimiter within a string, and Visual Basic fills a one-dimensional array with the substrings.

However, in addition to the functionality described above, you can also use the Split function as a quick way to determine the number of substrings within a larger string. For example, suppose you want to determine the number of times 注释:sea注释: appears in the string "She sells seashells by the seashore." To do so, simply perform the split, and use the UBound function to count the number of elements in the resultant array, as in

strTungTied = "She sells seashells by the seashore."
arySea = Split(strTungTied, "sea")
MsgBox "注释:Sea注释: appears in 注释:" & strTungTied _
& "注释: " & UBound(arySea) & " times."

When you run this code snippet, Visual Basic informs you that 注释:sea注释: appears in the phrase twice.