为什么字符串比较条件不成立?
我有在 text1中提取出 09010100 这些数‘text1内容--------------------------------
LUZ5] t.
时间:09a01.O1 00:46
h
EZ 1 1
‘-----------------------------------------------
Private Sub Command1_Click()
Dim nLen As Byte
On Error Resume Next
Const LDATA As String = "09010100"
For i = 1 To Len(Text1.Text)
If Asc(Mid(Text1.Text, i, 1)) > 58 Or Asc(Mid(Text1.Text, i, 1)) < 48 Then
Text1.Text = Replace(Text1.Text, Mid(Text1.Text, i, 1), "")
End If
Next i '为什么此语句不能够去掉所有不是数字的字符?
nLen = InStr(Text1.Text, "09")
Text1.Text = Mid(Text1.Text, nLen, 8)
If Text1.Text = LDATA Then '看这里字符串明明是等于"09010100"的字符串 为什么条件不成立?
MsgBox "ok"
Else
End If
End Sub --------------------编程问答-------------------- 注意:回车换行是看不到的,你的字符串含有回车换行,所以条件不成立,另外为读取那个数据,完全没必要这么麻烦,用split函数就行了 --------------------编程问答-------------------- 按LZ给出的text1内容
--------------------------------
LUZ5] t.
时间:09a01.O1 00:46
h
EZ 1 1
‘-----------------------------------------------
09a01.O1这部分, .后面的是字母O, 而不是数字0, 所以处理之后得到的是"09011004"而不是"09010100", 当然不相等的
另外, 处理部分单独写成函数比较好:
--------------------编程问答-------------------- 你的算法应该说不算是最优的,你不要让大家 ,根着你的算法走,
Option Explicit
Function StringProc(ByVal S As String) As String
Dim i As Integer, T As String
T = ""
For i = 1 To Len(S)
If Mid(S, i, 1) >= "0" And Mid(S, i, 1) <= "9" Then
T = T & Mid(S, i, 1)
End If
Next i
StringProc = T
End Function
Private Sub Command1_Click()
Dim nLen As Byte
On Error Resume Next
Const LDATA As String = "09010100"
Text1.Text = StringProc(Text1.Text)
nLen = InStr(Text1.Text, "09")
Text1.Text = Mid(Text1.Text, nLen, 8)
If Text1.Text = LDATA Then
MsgBox "ok"
Else
End If
End Sub
你要把你想实现的功能说出来,大家就会给你一个最优的算法,
你这样做不知你想做什么。 --------------------编程问答--------------------
Private Sub Command1_Click()
Dim strA As String
Dim arr() As String
Const LDATA As String = "09010100"
arr = Split(Trim(Text1.Text), vbCrLf)
strA = Replace(Mid(arr(1), 4), " ", "")
strA = Replace(strA, ".", "")
strA = Replace(strA, "a", "")
strA = Replace(strA, "O", "0")
strA = Mid(strA, 1, 8)
If strA = LDATA Then
MsgBox "OK"
End If
End Sub
补充:VB , 基础类