Re: InStr and striping NULs

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance




"Howard Kaikow" <kaikow@xxxxxxxxxxxxx> wrote in message
news:ONwSrPgIIHA.4688@xxxxxxxxxxxxxxxxxxxxxxx
Over the years, I've used several methods for eliminating trailing Nul
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
then
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.



.



Relevant Pages

  • Re: InStr and striping NULs
    ... Looping to remove trailing Nul characters by checking rightmost ... Using InStr to finfd leftmost Nul and then truncate string. ...
    (microsoft.public.vb.general.discussion)
  • Re: InStr and striping NULs
    ... Looping to remove trailing Nul characters by checking rightmost ... Using InStr to finfd leftmost Nul and then truncate string. ... using the cNul form causes k to always return 1. ...
    (microsoft.public.vb.general.discussion)
  • RE: How can I create a outlook appointment item from data in a e-m
    ... If you know the starting point of where the text you want occurs, use InStr ... Eric Legault (Outlook MVP, MCDBA, MCTS: ... if I have a string that looks like this: ... Once you get the text you need, it's easy to create a new appointment item: ...
    (microsoft.public.outlook.program_vba)
  • Parsing a string
    ... The function also has two optional input parameters, ... default delimiter value in the function definition line ... then using the instr() functions. ... Dim ArrayBreakPosAs Integer, strPrefix As String ...
    (microsoft.public.access.queries)
  • Re: Null character and JavaScript strings
    ... the end of each string literal denoted by single or double quotes. ... NUL character does terminate strings. ... In ECMAScript ... Alert doesn't get confused by embedded NULs. ...
    (comp.lang.javascript)