VB如何定义b使len(text1.text)=len(b)
如何判断出a的 真真实长度?
再如何定义变量b,使b=a的有效字符?
要求等同效果:text1.text=a 从而使len(text1.text)=len(b)
谢谢
不好意思 可能是我没说明白
Private Sub Command1_Click()
Dim a As Long, b As String * 100
a = FindWindow("Notepad", vbNullString)
GetWindowText a, b, 101
print b
‘b的真实长度是多少?
End Sub 展开
不好意思 我没说明白
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Sub Command1_Click()
Dim a As Long, b As String * 100
a = FindWindow("Notepad", vbNullString)
GetWindowText a, b, 101
Print b
'检验 b
Dim i As Integer
For i = 1 To 100
Print Asc(Mid(b, i, 1));
If i Mod 10 = 0 Then Print
Next
'发现 b 包含 空格Chr(32) 和 Chr(0)
'因此, 去空格, 去 Chr(0), 再 Len()
Print "b的真实长度为", Len(RTrim(Replace(b, Chr(0), "")))
End Sub
实验知道,定长字符串默认值是一串西文空格(ASCII码值为32)。
所以,这样理解:
所以,可以这样写代码,达到你的要求:
Dim a As String * 100
Dim b As String
Dim i As Integer
Dim n As Integer
a = "da0 1564"
For i = 100 To 1 Step -1
If Asc(Mid(a, i, 1)) <> 32 Then
n = i
Exit For
End If
Next i
b = Left(a, 8)
Print a
Print b
你这里用到的是API函数?
要得到B的实际长度,还是同样的道理呀!难道你没有看懂?
Private Sub Command1_Click()
Dim a As Long, b As String * 100
a = FindWindow("Notepad", vbNullString)
GetWindowText a, b, 101
print b
‘b的真实长度是多少?
Dim i as Integer
for i=100 to 1 step -1
if asc(mid(b,i))<>32 then
msgbox "长度为:" & i
exit for
end if
next i
end sub
Print Len(RTrim(Replace(b, Chr(0), "")))
其他情况下,你可以这样定义
Dim a as string
a = string(100,chr(0))
a中写入值后用api函数lstrlen可以获取它的长度