Results 1 to 7 of 7

Thread: How to remove null from fixed length string

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2012
    Posts
    433

    How to remove null from fixed length string

    There is a fixed length string which has 3 null char.
    I want to concatenate other string to this string but failed because 3 nulls.
    How can I do this?

    Here is sample code.

    Dim str1 As String * 10
    Dim str2 As String

    str1 = "ABCDEFG" & Chr(0) & Chr(0) & Chr(0)
    str2 = str1 + "HIJ"

    MsgBox str2 'str2 is ""ABCDEFG" not "ABCDEFGHIJ"

  2. #2
    Member
    Join Date
    May 2013
    Location
    Indonesia
    Posts
    33

    Re: How to remove null from fixed length string

    May be you can use this code:

    Dim str1 as string
    Dim str2 as string
    str1 = "ABCDEFG"
    str2 = str1 + "HIJ"

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2012
    Posts
    433

    Re: How to remove null from fixed length string

    Quote Originally Posted by Vincent Hadison View Post
    May be you can use this code:

    Dim str1 as string
    Dim str2 as string
    str1 = "ABCDEFG"
    str2 = str1 + "HIJ"
    Actually str1 is acquired from some C DLL function which requires 10 fixed length string.
    I just made sample code to explaine my issue.
    So I can't declare str1 As String

  4. #4
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: How to remove null from fixed length string

    How about:
    Code:
    Option Explicit
    
    Private Declare Function lstrlen Lib "kernel32" Alias "lstrlenW" ( _
        ByVal lpString As Long) As Long
    
    Private Sub Command1_Click()
        Dim str1 As String * 10
        Dim str2 As String
        Dim NullPos As Long
    
        str1 = "ABCDEFG" & String$(3, 0)
    
        'Method 1:
        str2 = Replace$(str1, vbNullChar, "") & "HIJ"
        MsgBox CStr(Len(str2)) & vbNewLine _
             & "[" & str2 & "]", , _
               "Method 1"
    
        'Method 2:
        NullPos = InStr(str1, vbNullChar)
        If NullPos > 0 Then
            str2 = Left$(str1, NullPos - 1) & "HIJ"
        Else
            str2 = str1 & "HIJ"
        End If
        MsgBox CStr(Len(str2)) & vbNewLine _
             & "[" & str2 & "]", , _
               "Method 2"
    
        'Method 3:
        str2 = Left$(str1, lstrlen(StrPtr(str1))) & "HIJ"
        MsgBox CStr(Len(str2)) & vbNewLine _
             & "[" & str2 & "]", , _
               "Method 3"
    End Sub
    The last method using an API call works by relying on the hidden "out of band" NUL that is stored after every String's contents. These are stored as part of a BSTR (OLE String, i.e. VB String) just to make them easier to pass to an API call as a "C string pointer" (lpsz).
    Last edited by dilettante; May 20th, 2013 at 08:31 PM.

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: How to remove null from fixed length string

    HIJ doesn't get appended because the string is a fixed length and it is already full... there's 3 nulls because the string itself only has 7 characters in it. I'm trying to decide which of the three methods dilettante posted I like best and is the most flexible going forward... probably method 2, although 3 is the least amount of code... I mention this because I doubt you're getting "ABCDEFG" from anything and that "HIJ" is just as arbitrary and contrived as the data sample, and you're going to want something flexible to handle "A" as well as "ABCDE" and "ABCDEFH"


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: How to remove null from fixed length string

    This appears to work
    Code:
    Dim strA As String * 10
    Dim strB As String
    strA = "ABCDEFG" & Chr(0) & Chr(0) & Chr(0)
    strB = Replace(strA & "HIJ", Chr(0), vbNullString)
    Debug.Print strB
    EDIT: On reflection this looks like Dilettante's Method 1

  7. #7
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: How to remove null from fixed length string

    Quote Originally Posted by jdy0803 View Post
    Actually str1 is acquired from some C DLL function which requires 10 fixed length string.
    I just made sample code to explaine my issue.
    So I can't declare str1 As String
    I believe what you just need is a string buffer. In that case, a dynamic string would be good enough. Typically, such a string is filled first with a sufficient number of characters (such as spaces or nulls) before it is passed to the external function. Can you show the C function's signature and your VB6 declaration?
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width