VB中怎样将一个字符串一个字符一个字符的比较?
VB中怎样将一个字符串一个字符一个字符的比较?
VB中好像不能用字符数组,只能用字符串数组!故而在下不能进行单个字符的比较?
麻烦各位大侠给小弟指点迷津!
[解决办法]
提供一个思路给你:
每次敲键盘的时候都会产生一个键盘事件,取键盘的ASC码,与正确的字符ASC码进行比较就行了。
[解决办法]
一个个取出来比较
mid(str,i,1)
[解决办法]
Option ExplicitPrivate Declare Function MessageBox Lib "user32" Alias "MessageBoxA" (ByVal hwnd As Long, _ ByVal lpText As String, _ ByVal lpCaption As String, _ ByVal wType As Long) As LongPrivate Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDest As Any, pSrc As Any, _ ByVal ByteLen As Long)Private Sub Command1_Click() Dim strOne As String '字符串1 Dim strTwo As String '字符串2 Dim lngADDstrOne As Long '字符串1的地址 Dim lngADDstrTwo As Long '字符串2的地址 Dim byteOne As Byte ' Dim byteTwo As Byte ' Dim bolExit As Boolean '退出比较循环的标志 Dim intP As Integer 'On Error GoTo errSub strOne = Text1.Text strTwo = Text2.Text If strOne = "" Or strTwo = "" Then Exit Sub If Len(strOne) <> Len(strTwo) Then MessageBox hwnd, "字符串长度不一致!", "系统提示", vbOKOnly + vbExclamation Exit Sub End If '开始按字节逐一比较字符串 '比如:字符串:123在内存中的存储形式时:49 00 50 00 51 00 所以读取奇数位置的字符比较 lngADDstrOne = StrPtr(strOne) '读取字符串1的地址 lngADDstrTwo = StrPtr(strTwo) '读取字符串2的地址 Do CopyMemory ByVal VarPtr(byteOne), ByVal (lngADDstrOne + intP), 1 CopyMemory ByVal VarPtr(byteTwo), ByVal (lngADDstrTwo + intP), 1 If (byteOne <> byteTwo) Then bolExit = True End If intP = intP + 2 Loop Until bolExit Or (intP \ 2 > Len(strOne)) If bolExit Then MessageBox hwnd, "字符串内容不一致!", "系统提示", vbOKOnly + vbExclamation Else MessageBox hwnd, "字符串内容一致!", "系统提示", vbOKOnly + vbExclamation End If Exit SuberrSub:End SubPrivate Sub Form_Load() Text1.Text = "" Text2.Text = "" Command1.Caption = "按字节比较字符串"End Sub
[解决办法]
VB的确没有提供对字符串中字符的直接操作方法,也有高人找到了把字符串当做数组进行操作的方法,详见<<Visual Basic 高级编程>>一书.不过也有一种变通的方法.即把字符串分给字节数组.利用字节数组进行比较.不过要注意,VB中字符采用Unicode编码,为两个字节.因为在数组中每两个元素代表一个字符.
[解决办法]
Visual Basic高级编程:http://download.csdn.net/source/1371398
VB的字符串时UniCode格式的,每一个字符占用2个字节,所以使用数组来存储字符串的话,每个字符的高位必须是:&H00,比如:A,在内存中的存储如此:&H00 &H40
可以使用StrPtr函数来获得字符串的首地址、VarPtr获得变量的地址;CopyMemory函数可以通过地址来读取或写内存。
[解决办法]
使用数组进行字符串比较的源码,呵呵。
public function CompByteString(S1 as string,S2 as string) as longDim S1 as string,S2 as string,c1() as byte,c2() as byte,i as long'在此输入S1,S2两个字符串的值c1=strconv(s1,vbFromUnicode)c2=strconv(s2,vbFromUnicode)for i=0 to ubound(c) if c1(i)<>c2(i) then CompByteString=CompByteString+1 debug.print i '在此更改为处理语句 end ifnextend function
[解决办法]
Dim S1 as string,S2 as string红色字去掉,忘记去了。
[解决办法]
Option Explicit
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Sub Command1_Click()
Dim str As String
str = "中国人民万岁"
Dim str1 As String
str1 = "VB语言万岁"
Dim CharArr() As Integer
ReDim CharArr(Len(str) - 1)
CopyMemory CharArr(0), ByVal StrPtr(str), Len(str) * 2
Dim CharArr1() As Integer
ReDim CharArr1(Len(str1) - 1)
CopyMemory CharArr1(0), ByVal StrPtr(str1), Len(str1) * 2
Dim i As Long
Dim nFlag As Long
nFlag = 0
Do While i < Len(str) And i < Len(str1)
If (CharArr(i) = CharArr1(i)) Then
Else
End If
Loop
End Sub
[解决办法]