读取INI文件时使用变量
Dim strTest As String
Dim strReadBuff As String * 256...
strTest = strReadBuff
...我想让strTest没有空格,也就是没有256位那么长!请问怎样能去掉这个字符串的空格啊?用trim()去不掉!
Dim strTest As String
Dim strReadBuff As String * 256...
strTest = strReadBuff
...我想让strTest没有空格,也就是没有256位那么长!请问怎样能去掉这个字符串的空格啊?用trim()去不掉!
解决方案 »
- 获取VB的变量类型
- 怎么让他不影响向文本框中输入内容的速度?
- html help workshop所作帮助文件中目录字的背景色问题
- 关于mscomm控件
- MSCommLib.MScomm是什么东东,用来干什么使得?
- 一个奇怪的问题,关于rs.Open sql,conn,3,在线等.急啊!
- 关于access+word的问题(高分求助)
- 请问16进制数字转换成十进制数字的函数是什么?谢谢!
- 编好的VB程序如何制作key盘(软盘)
- 请教各位大虾:如何用控件实现文件上载
- 怎么使VB连接Oracle临时表的Connection Session和一个Word文件中Mail Merge连接同一临时表的Connection Session是一个Session..????(解决
- 新手求助:VBA中怎样比较字符串的大小?
strReadBuff读取的是"sa"
strTest=replace(strTest,Chr(0),"")
结果得到的是"saeng"
strReadBuff读取的是"25"
strTest=replace(strTest,Chr(0),"")
结果得到的是"25456"不知道为什么
Public Function GetIniItem(ByVal sApp As String, ByVal sKey As String, Optional ByVal sIniName As String = "") As String
Dim lReturn As Long, sBuffer As String, sFileName As String, c As Long, sValue As String
On Error Resume Next
If Len(sIniName) = 0 Then
sFileName = App.Path & "Config.ini"
Else
sFileName = App.Path & sIniName
End If
sBuffer = String(1024, 0)
lReturn = GetPrivateProfileString(sApp, sKey, "", sBuffer, 1024, sFileName)
c = InStr(1, sBuffer, Chr(0)) - 1
If c > 0 Then sValue = Left$(sBuffer, c) Else sValue = ""
GetIniItem = sValue
End Function
chr(0) 显示为一个空白,不表示它是空格。*****
strReadBuff读取的是"25"
strTest=replace(strTest,Chr(0),"")
结果得到的是"25456"
*****你怎么知道它读取的是 "25"?你看到的不等于它的实际值。
最好显示 ascii 串,看看它里面到底是什么?x=""
for i=1 to len(strTest)
x= x & "," & asc(mid(strTest,i,1))
next这样 x 会得到一个类似 ,32,0,65,... 之类的串,这才是实际的内容。
ascii 低于 32 或高于 127 的,在“立即”窗中都表现为空白(格)。
strTest=replace(strTest,Chr(0),"")
结果得到的是"saeng"
strReadBuff读取的是"25"
strTest=replace(strTest,Chr(0),"")
结果得到的是"25456"可能是Chr(0)之后仍有数据。如果希望去掉Chr(0)之后的东西。
strTest=left(strTest,InStrRev(strTest,Chr(0)))
然后
strTest=replace(strTest,Chr(0),"")
strTest=left(strTest,InStr(strTest,Chr(0))-1)
strBuff = Left(strBuff, InStr(strBuff, Chr(0)) - 1)就可以了
Option Explicit
Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
Public Function GetINI(strINIFile As String, strSection As String, _
strKey As String, strDefault As String)
On Error GoTo ErrMsg
Dim strTemp As String
Dim intLength As Integer
'判断INI文件是否存在
If Dir(strINIFile) = "" Then
MsgBox "INI文件“" & strINIFile & "”已被损坏,请联系管理人员或开发人员!", vbExclamation, "警告!"
' RepairINIFile strINIFile
Exit Function
End If
strTemp = Space$(256)
intLength = GetPrivateProfileString(strSection, strKey, strDefault, strTemp, 255, strINIFile)
GetINI = Left$(strTemp, intLength)
Exit Function
ErrMsg:
MsgBox "从INI文件“" & strINIFile & "”文件读取数据时出现异常,请检查是否删除或移动了该文件!" & vbCrLf & _
Err.Description, vbExclamation, "提示"
End FunctionPublic Function WriteINI(strINIFile As String, strSection As String, _
strKey As String, strValue As String) As Boolean
On Error GoTo ErrMsg
Dim n As Integer
WriteINI = False
'Replace any CR/LF characters with spaces
If Len(strValue) >= 1 Then
For n = Len(strValue) To 1
If Mid$(strValue, n, 1) = vbCr Or Mid(strValue, n, 1) = vbLf Then
Mid$(strValue, n, 1) = ""
End If
Next n
End If
n = WritePrivateProfileString(strSection, strKey, strValue, strINIFile)
WriteINI = True
Exit Function
ErrMsg:
MsgBox "向INI文件“" & strINIFile & "”文件写入数据时出现异常,请检查是否删除或移动了该文件!" & vbCrLf & _
Err.Description, vbExclamation, "提示"
End Function
Dim strTest As String
Dim strReadBuff As String * 256
private Function Getrealstr( strReadbuffPara as string) as string
dim i as integer
dim strtemp as string i=instr(1,strReadbuffPara," ")
strTemp=Mid(strReadBuffPara,1,i-1)
GetRealstr=strtempEnd functionPrivate sub xxx_xxx()
strTest =Getrealstr(strReadBuff )
end sub
先调用: strReadBuff= Space(MAX_PATH) ..... strTest = Trim$(strReadBuff)============================================这个方法是对的。
凡是 Dim s as string * 256 这种方法,除非重新使用一个变量,否则总是有空格!
Dim xStr As String * 256
xStr = "abc" & Chr(0) & "def" & Chr(0) & "hijk"
test = Split(xStr, Chr(0))
For i = 0 To UBound(test)
Debug.Print test(i)
Next i
End
End Sub