Re: InStr and striping NULs
- From: "Ralph" <nt_consulting64@xxxxxxxxx>
- Date: Thu, 8 Nov 2007 07:30:57 -0600
"Howard Kaikow" <kaikow@xxxxxxxxxxxxx> wrote in message
news:ONwSrPgIIHA.4688@xxxxxxxxxxxxxxxxxxxxxxx
Over the years, I've used several methods for eliminating trailing Nulthen
characters, including:
1. Using API functions.
2. Looping to remove trailing Nul characters by checking rightmost
caharacter.
3. Using InStr to finfd leftmost Nul and then truncate string.
Recently, I decided to create a Public variable cNul = Chr$(0), which I
used
instead of Chr$(0) just about everywhere.
I have many cases of the following code snippet.
Filename = WFD.cFileName
' k = InStr(Filename, cNul)
k = InStr(Filename, Chr$(0))
If k > 1 Then
Filename = Mid$(Filename, 1, k - 1)
End If
In one program, using the cNul form causes k to always return 1.
If InStr considers cNul to have 0 length, why not Chr$(0)?
This raises three questions:
1. I guess that cNul is passed by reference.. Why would Chr$(0) be passed
differently.
2. Most puzzling is that identical code is used in 7 programs and the
problem occurs only in one program.
3. Should I drop the InStr method for getting rid of Nuls?
Others will be along to flesh this out, but for now consider that in VB
there are no "chars", there are only Strings - fixed or dynamic. Chr$
returns a String. It may actually contain one or two bytes depending on if
the string is Unicode or not.
The behavior of " k = InStr(Filename, cNul)" to return 1 suggests Filename
is a unicode string and the first nul is being caught for some reason.
How is "Public variable cNull" declared?
What O/S is the errant box using?
Instead of creating your own definition take a look at vbNullString or
vbNull.
Here is the over-built VB way for a fixed string ending in nuls...
Public Function StripNulls(OriginalStr As String) As String
If (InStr(OriginalStr, Chr(0)) > 0) Then
OriginalStr = Left(OriginalStr, _
InStr(OriginalStr, Chr(0)) - 1)
End If
StripNulls = OriginalStr
End Function
Unless this is a routine that will be called often or you have to deal with
embedded nuls, I'd stick with the routine above.
.
- Follow-Ups:
- Re: InStr and striping NULs
- From: Randy Birch
- Re: InStr and striping NULs
- From: Ralph
- Re: InStr and striping NULs
- References:
- InStr and striping NULs
- From: Howard Kaikow
- InStr and striping NULs
- Prev by Date: Re: InStr and striping NULs
- Next by Date: Re: InStr and striping NULs
- Previous by thread: Re: InStr and striping NULs
- Next by thread: Re: InStr and striping NULs
- Index(es):
Relevant Pages
|