VB中字符串匹配的多种方式
这段时间在移植项目的过程中,起初我想判断一个字符串中是否存在某字符(串),直接的使用方法是用instr(start,string1,string2,VB compare),但经过分析在VB中可以只用一下方法来判断!
1.常用的INStr方法
Function InStr([Start], [String1], [String2], [Compare As VbCompareMethod = vbBinaryCompare])
VBA.Strings 的成员
返回在另一字符串中第一次出现某一字符串的位置
判断string1中从start开始的位置第一次出现string2的位置
2.使用正则表达式
在VB中需要引入Library VBScript_RegExp_55
位置在C:\WINDOWS\System32\vbscript.dll\3
Microsoft VBScript Regular Expressions 5.5
当然,你可以直接在项目中引用,使用方法如下:
Dim ResultString As String
Dim myRegExp As RegExp
Dim resultCollection As MatchCollection
Const pattern As String = "^\s*Dim\s+([\w|,|\s]+)(?:\s+As\s+Recordset)"
Public Function executes(SubjectString As String, result As MatchCollection)
Set myRegExp = New RegExp
myRegExp.MultiLine = True
myRegExp.Global = True
myRegExp.pattern = pattern
Set result = myRegExp.Execute(Trim(SubjectString))
End Function
3.第三种是比较特别的方法,是采用if string like [parttern],即string需要满足parttern中的模式,才会返回true,其具体定义如下:
语法
result = string Like pattern
Like 运算符的语法具有以下几个部分:
部分 描述
result 必需的;任何数值变量。
string 必需的;任何字符串表达式。
pattern 必需的;任何字符串表达式,遵循“说明”中的模式匹配约定。
说明
如果 string 与 pattern 匹配,则 result 为
相关文档:
接着外挂教程 VB 从零开始编外挂
需要VBAPI函数:
FindWindow←寻找窗口列表中第一个符合指定条件的顶级窗口
GetWindowThreadProcessId←获取与指定窗口关联在一起的一个进程和线程标识符
--------------------------------------------------------------------------------------------------------------- ......
在C语言中,escape的符号很好用,
比如
"中国一定强"
这个字串可以写成:
"\x4E2D\x56FD\x4E00\x5B9A\x5F3A"
用字元编码编写程序,在其他不同语言的windows运作时,比较不会有问题。
(我尽量不想在程序中写入中文)
但是vb如果全部要用字元编码写的话,就会很麻烦而且一个一个都要手写成:
ChrW(&H4E2D) ......
VB中 Replace 函数
描述
返回字符串,其中指定数目的某子字符串被替换为另一个子字符串。
语法
Replace(expression, find, replacewith[, compare[, count[, start]]])
Replace 函数的语法有以下参数:
参数 描述
expression 必选。字符串表达式,包含要替换的子字符串。
find 必选。被搜索的子字符串。
......
Option Explicit
Private rsMain As ADODB.Recordset
Private rsTerm As ADODB.Recordset
Private strSql As String
Private Sub cmdAbout_Click()
frmAbout.Show
End Sub
Private Sub cmdAddObject_Click() '程序段
......