|
-
May 20th, 2013, 06:38 PM
#1
Thread Starter
Hyperactive Member
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"
-
May 20th, 2013, 06:42 PM
#2
Member
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"
-
May 20th, 2013, 06:58 PM
#3
Thread Starter
Hyperactive Member
Re: How to remove null from fixed length string
 Originally Posted by Vincent Hadison
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
-
May 20th, 2013, 07:20 PM
#4
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.
-
May 20th, 2013, 08:14 PM
#5
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
-
May 20th, 2013, 11:25 PM
#6
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
-
May 21st, 2013, 01:41 AM
#7
Re: How to remove null from fixed length string
 Originally Posted by jdy0803
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|