读取INI文件时使用变量
Dim strTest As String
Dim strReadBuff As String * 256...
strTest = strReadBuff
...我想让strTest没有空格,也就是没有256位那么长!请问怎样能去掉这个字符串的空格啊?用trim()去不掉!

解决方案 »

  1.   

    strTest=replace(strTest," ","")
      

  2.   

    这样不行啊长度还是256!这里的空格不是" "而是实体的CHR(0)我也试过strTest=replace(strTest,Chr(0),"")但是这样得到的结果有进不正确!比如
    strReadBuff读取的是"sa"
    strTest=replace(strTest,Chr(0),"")
    结果得到的是"saeng"
    strReadBuff读取的是"25"
    strTest=replace(strTest,Chr(0),"")
    结果得到的是"25456"不知道为什么
      

  3.   


    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
      

  4.   

    晕,那你的问题应该是去掉不可见字符?怎么说是去掉空格?
    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 的,在“立即”窗中都表现为空白(格)。
      

  5.   

    S = LEFT(S,INSTR(CHR(0),S)-1)
      

  6.   

    strReadBuff读取的是"sa"
    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),"")
      

  7.   

    或者
    strTest=left(strTest,InStr(strTest,Chr(0))-1)
      

  8.   

    呵~,上面说得太麻烦了,
    strBuff = Left(strBuff, InStr(strBuff, Chr(0)) - 1)就可以了
      

  9.   

    封装了读写ini文件的函数的模块(读取时已经解决了楼主的问题):
    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
      

  10.   

    先调用:    strReadBuff= Space(MAX_PATH).....strTest = Trim$(strReadBuff)
      

  11.   

    兄弟,这样吧
    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
      

  12.   

    使用Trim()函数或者Replace()函数都可以解决,具体应用可以查看MSDN
      

  13.   

    回复人: liuyan4794(青牛) ( ) 信誉:101  2004-07-06 15:50:00  得分: 0  
     
       先调用:    strReadBuff= Space(MAX_PATH)       .....      strTest = Trim$(strReadBuff)============================================这个方法是对的。
    凡是 Dim s as string * 256 这种方法,除非重新使用一个变量,否则总是有空格!
      
     
      

  14.   

    Private Sub Form_Load()
    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