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 为
相关文档:
文章来源: http://www.zoesan.com By Error 302777528转载请注明出处
以上vb用到指针技术查找字符串与c#一般的.indexof()查找相比较,明显看出谁快谁慢。
VB:
Option Explicit
'指针方法操作字符串
'Copy一个字符串到缓存中
Public Declare Function GetTickCount Lib "kernel32" () As Long
Public ......
VC函数是:extern "C" int __declspec(dllexport)PassPortRead(char *InPutData,char OuPutData[255]);
VB声明是:Private Declare Function PassPortRead Lib "PPRead.dll" (ByVal InPutData As String, ByVal OutPutData As String) As Integer
生成的文件能正常运行,并且能生成相应数据,但是在调试时提示"DLL调用约定错 ......
Introduction
This article is about passing data between VB.NET/C# WinForms and JavaScript.
Before reading further, let me warn you that this article is not about ASP.NET. Concepts covered in the article are applied to Desktop applications.
Background
I was working on a project which required dat ......
※==================================================================
※本连载文章说明:
※1、连载首发于《软件报》(http://www.sweek.com)2006年21期(2006年5月22日);
※2、此次网上连载采用的是原稿件结构,内容与《软件报》发表略有不同;
※3、谢绝除《软件报》及其相关刊物之外的传统媒体部分或全部转载 ......
Option Explicit
Private Const INTERNET_OPEN_TYPE_DIRECT = 1
Private Const scuseragent = "vb wininet"
Private Const INTERNET_FLAG_PASSIVE = &H8000000
'调用设置环境
Private Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" (ByVal sAgent As String, ByVal lAccessType As Long ......