Connecting Tech Pros Worldwide Help | Site Map

Bug: string.substring.trim = "" (boolean expression issue)

Darren Anderson
Guest
 
Posts: n/a
#1: Nov 21 '05
I have a function that I've tried using in an if then statement and I've
found that no matter how much reworking I do with the code, the expected
result is incorrect.

the code:

If Not (strIn.Substring(410, 10).Trim = "") Then
'Something processed
Else
'Something processed
End If

The string.substring(410,10).trim is equivelent to an emptry string, e.g.
"", when it comes in. So the expected result is supposed to be TRUE when you
use (strIn.Substring(410, 10).Trim = ""), and I use the NOT keyword which
negates the true to a false (or vice versa). I can not get this DAMN
function to give me the proper boolean expression, is this a LOGIC bug...

I've tried using string.substring.trim = space(?) and I've tried
string.substring.trim <> "" and none of these will yield the correct boolean
value in an If...Then context
Herfried K. Wagner [MVP]
Guest
 
Posts: n/a
#2: Nov 21 '05

re: Bug: string.substring.trim = "" (boolean expression issue)


"Darren Anderson" <DarrenAnderson@discussions.microsoft.com> schrieb:[color=blue]
>I have a function that I've tried using in an if then statement and I've
> found that no matter how much reworking I do with the code, the expected
> result is incorrect.
>
> the code:
>
> If Not (strIn.Substring(410, 10).Trim = "") Then
> 'Something processed
> Else
> 'Something processed
> End If
>
> The string.substring(410,10).trim is equivelent to an emptry string, e.g.
> "", when it comes in. So the expected result is supposed to be TRUE when
> you
> use (strIn.Substring(410, 10).Trim = ""), and I use the NOT keyword which
> negates the true to a false (or vice versa). I can not get this DAMN
> function to give me the proper boolean expression, is this a LOGIC bug...[/color]

Your code works just fine for me. The condition in 'If...Then' is true if
the trimmed string is not empty (zero-length string).

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Jay B. Harlow [MVP - Outlook]
Guest
 
Posts: n/a
#3: Nov 21 '05

re: Bug: string.substring.trim = "" (boolean expression issue)


Darren,
First "'Select isn't Broken!" :-)

http://www.pragmaticprogrammer.com/p...rule_list.html


Do you have any sample code that demonstrates the perceived problem?

[color=blue]
> The string.substring(410,10).trim is equivalent to an empty string, e.g.
> "",[/color]
If you assign string.substring(410,10).trim to a variable, what is in the
variable? What is the length of the variable?

Where is strIn coming from? Does it have ChrW(0) characters in it?

ChrW(0) will make the string appear to be "" in the debugger, when its
actually not, as ChrW(0) is a valid string.

If you run the following what do you see?

Dim strIn As String = New String(" "c, 450)

Dim x As String = strIn.Substring(410, 10).Trim()
Debug.WriteLine(x, "x")
Debug.WriteLine(x.Length, "x.length")

Debug.WriteLine(Not (x.Trim() = ""), "not (x.trim = space)")
Debug.WriteLine(x.Trim() <> "", "x.trim <> space")
Debug.WriteLine(x.Trim() = "", "x.trim = space")

Debug.WriteLine(Not (strIn.Substring(410, 10).Trim() = ""), "not
(substring.trim = space)")
Debug.WriteLine(strIn.Substring(410, 10).Trim() <> "",
"substring.trim <> space")
Debug.WriteLine(strIn.Substring(410, 10).Trim() = "",
"substring.trim = space")

What do you see if you use your original string instead of "New String(" "c,
450)" for strIn?

Hope this helps
Jay


"Darren Anderson" <DarrenAnderson@discussions.microsoft.com> wrote in
message news:963E2040-E02A-4880-BC3B-8395BB25E2A4@microsoft.com...[color=blue]
>I have a function that I've tried using in an if then statement and I've
> found that no matter how much reworking I do with the code, the expected
> result is incorrect.
>
> the code:
>
> If Not (strIn.Substring(410, 10).Trim = "") Then
> 'Something processed
> Else
> 'Something processed
> End If
>
> The string.substring(410,10).trim is equivelent to an emptry string, e.g.
> "", when it comes in. So the expected result is supposed to be TRUE when
> you
> use (strIn.Substring(410, 10).Trim = ""), and I use the NOT keyword which
> negates the true to a false (or vice versa). I can not get this DAMN
> function to give me the proper boolean expression, is this a LOGIC bug...
>
> I've tried using string.substring.trim = space(?) and I've tried
> string.substring.trim <> "" and none of these will yield the correct
> boolean
> value in an If...Then context[/color]


Phill. W
Guest
 
Posts: n/a
#4: Nov 21 '05

re: Bug: string.substring.trim = "" (boolean expression issue)


"Darren Anderson" <DarrenAnderson@discussions.microsoft.com> wrote in
message news:963E2040-E02A-4880-BC3B-8395BB25E2A4@microsoft.com...[color=blue]
> I have a function that I've tried using in an if then statement and
> I've found that no matter how much reworking I do with the code,
> the expected result is incorrect.
>
> If Not (strIn.Substring(410, 10).Trim = "") Then[/color]

Are you sure the substring contains /exactly/ 10 /spaces/?
Could any of them be Null or other whitespace characters?
Trim() will only remove spaces.

Is strIn /at least/ 419 characters long? VB will get upset if you
try to slice out a substring that's doesn't fit within the source string,
as in

"abc".SubString( 2, 2 ) ' fails

You're /not/ using "On Error Resume Next", /are/ you?

HTH,
Phill W.


Jay B. Harlow [MVP - Outlook]
Guest
 
Posts: n/a
#5: Nov 21 '05

re: Bug: string.substring.trim = "" (boolean expression issue)


Phill,[color=blue]
> Are you sure the substring contains /exactly/ 10 /spaces/?
> Could any of them be Null or other whitespace characters?
> Trim() will only remove spaces.[/color]
Interesting, according to MSDN String.Trim() will remove whitespace
characters, it suggests it is using Char.IsWhiteSpace to determine what is a
whitespace character!

http://msdn.microsoft.com/library/de...trimtopic1.asp

Testing shows (see below) that String.Trim() works with: ChrW(9), ChrW(10),
ChrW(11), ChrW(12), ChrW(13), ChrW(32), ChrW(12288)

However try any of the following and it fails:
ChrW(133), ChrW(160), ChrW(5760), ChrW(8192), ChrW(8193)
ChrW(8194), ChrW(8195), ChrW(8196), ChrW(8197), ChrW(8198)
ChrW(8199), ChrW(8200), ChrW(8201), ChrW(8202), ChrW(8203)
ChrW(8232), ChrW(8233), ChrW(8239)

According to Char.IsWhiteSpace all of the above are whitespace characters!

ChrW(133) is the "NEL" or Next Line control char. ChrW(160) is a non
breaking space
http://www.unicode.org/charts/PDF/U0080.pdf

CharW(5760) is the Ogham space mark.
http://www.unicode.org/charts/PDF/U1680.pdf

CharW(8192) to ChrW(8239) are various width spaces:
http://www.unicode.org/charts/PDF/U2000.pdf


Sample code I used to test with:

Const format As String = "ChrW({0})"
Dim sb As New StringBuilder
For charCode As Integer = AscW(Char.MinValue) To AscW(Char.MaxValue)
Dim ch As Char = ChrW(charCode)
If Char.IsWhiteSpace(ch) Then
sb.Append(ch)
Debug.WriteLine(String.Format(format, charCode),
"IsWhiteSpace")
End If
Next

Dim str As String = sb.ToString().Trim()
Debug.WriteLine(str, "str")
Debug.WriteLine(str.Length, "str.length")
Debug.WriteLine(Nothing)
For Each ch As Char In str
Dim charCode As Integer = AscW(ch)
Debug.WriteLine(String.Format(format, charCode), "not trimmed")
Next

If you really needed them trimmed a workaround would be to use
String.Trim(Char())

Static whitespace() As Char = {ChrW(&H9), ChrW(&HA), ChrW(&HB), _
ChrW(&HC), ChrW(&HD), ChrW(&H20), ChrW(&H85), ChrW(&HA0), _
ChrW(&H1680), ChrW(&H2000), ChrW(&H2001), ChrW(&H2002), _
ChrW(&H2003), ChrW(&H2004), ChrW(&H2005), ChrW(&H2006), _
ChrW(&H2007), ChrW(&H2008), ChrW(&H2009), ChrW(&H200A), _
ChrW(&H200B), ChrW(&H2028), ChrW(&H2029), ChrW(&H202F), _
ChrW(&H3000)}

Dim str As String = sb.ToString().Trim(whitespace)

Hope this helps
Jay


"Phill. W" <P.A.Ward@o-p-e-n-.-a-c-.-u-k> wrote in message
news:cvht31$l1p$1@yarrow.open.ac.uk...[color=blue]
> "Darren Anderson" <DarrenAnderson@discussions.microsoft.com> wrote in
> message news:963E2040-E02A-4880-BC3B-8395BB25E2A4@microsoft.com...[color=green]
>> I have a function that I've tried using in an if then statement and
>> I've found that no matter how much reworking I do with the code,
>> the expected result is incorrect.
>>
>> If Not (strIn.Substring(410, 10).Trim = "") Then[/color]
>
> Are you sure the substring contains /exactly/ 10 /spaces/?
> Could any of them be Null or other whitespace characters?
> Trim() will only remove spaces.
>
> Is strIn /at least/ 419 characters long? VB will get upset if you
> try to slice out a substring that's doesn't fit within the source string,
> as in
>
> "abc".SubString( 2, 2 ) ' fails
>
> You're /not/ using "On Error Resume Next", /are/ you?
>
> HTH,
> Phill W.
>
>[/color]


Darren Anderson
Guest
 
Posts: n/a
#6: Nov 21 '05

re: Bug: string.substring.trim = "" (boolean expression issue)




"Jay B. Harlow [MVP - Outlook]" wrote:
[color=blue]
> Darren,
> First "'Select isn't Broken!" :-)
>
> http://www.pragmaticprogrammer.com/p...rule_list.html
>[/color]
[...][color=blue]
>
> What do you see if you use your original string instead of "New String(" "c,
> 450)" for strIn?
>
> Hope this helps
> Jay
>[/color]



"Jay B. Harlow [MVP - Outlook]" wrote:
[color=blue]
> Darren,
> First "'Select isn't Broken!" :-)
>
> http://www.pragmaticprogrammer.com/p...rule_list.html
>
>[...]
>
> Hope this helps
> Jay[/color]

Jay,
Thanks it did help!

After going through and debugging it and looking at the actual characters
within the string, I found that TRIM wasn't eliminating chr(0) from the
string. I find it extremely frustrating that something simple like removal
of an unused character is so hard... SO I wrote a real 'TRIM' function.
I'll probably modify it more over time, but I'm in the middle of a project;
again thanks.


-------------------------------
'Use this function to remove chr(0) and chr(32) at beginning and end of string
' Function takes 1 input of string type and outputs string type, without
excessive characters
Public Function CleanUpString(ByVal strIn As String) As String
Dim nStartPos As Integer, nEndPos As Integer 'For finding beginning and
end of string
Dim nLen As Integer 'Length of string

CleanUpString = vbNullString 'Start with an empty string

If strIn.Length = 0 Then Exit Function 'Dummy check

nStartPos = 0 'Starting position is at the beginning of the string

'Find the actual beginning of the string
Do Until (Not strIn.ToCharArray(nStartPos, 1) = Chr(0) And Not
strIn.ToCharArray(nStartPos, 1) = Chr(32))
nStartPos += 1 'iterate forward
If nStartPos >= Len(strIn) Then Exit Do 'Exit if the whole string is
chr(0) or chr(32)
Loop

If (nStartPos >= Len(strIn)) Then Exit Function 'If the whole string is
nothing but chr(0) and chr(32) then return "" (a 'real' empty string)

nEndPos = (Len(strIn) - 1) 'Set the ending position at the end of string

'Find the actual end of the string
Do Until (Not strIn.ToCharArray(nEndPos, 1) = Chr(0) And Not
strIn.ToCharArray(nEndPos, 1) = Chr(32))
nEndPos -= 1 'iterate backward
If nEndPos <= 0 Then Exit Do
Loop

nLen = Len(strIn) - (((Len(strIn) - 1) - nEndPos) + nStartPos) 'Get the
length of important characters in the string

CleanUpString = strIn.Substring(nStartPos, nLen) 'Get the "important"
string for output

End Function
-------------------------------------

Darren

Jay B. Harlow [MVP - Outlook]
Guest
 
Posts: n/a
#7: Nov 21 '05

re: Bug: string.substring.trim = "" (boolean expression issue)


Darren,
Here's a simplified version of your function:

' Use this function to remove chr(0) and chr(32) at beginning and end of
string
' Function takes 1 input of string type and outputs string type, without
' excessive characters
Public Function CleanUpString(ByVal strIn As String) As String
Return strIn.Trim(ChrW(0), ChrW(32))
End Function


Also you can index into the string rather then converting it to a CharArray.
[color=blue]
> Do While (strIn.Chars(nStartPos) = Chr(0) OrElse strIn.Chars(nStartPos)
> = Chr(32))[/color]

Calling ToCharArray, twice, during each iteraction of the loop, in both
loops, may cause excessive strain on the Garbage Collector. As you are
creating a number of temporary arrays...

Note I find positive logic to be more readable then negative logic, hence I
"flipped" your condition.


Hope this helps
Jay

"Darren Anderson" <DarrenAnderson@discussions.microsoft.com> wrote in
message news:2B86B29F-A877-46CF-8453-C08AE1FCFD6B@microsoft.com...[color=blue]
>
>
> "Jay B. Harlow [MVP - Outlook]" wrote:
>[color=green]
>> Darren,
>> First "'Select isn't Broken!" :-)
>>
>> http://www.pragmaticprogrammer.com/p...rule_list.html
>>[/color]
> [...][color=green]
>>
>> What do you see if you use your original string instead of "New String("
>> "c,
>> 450)" for strIn?
>>
>> Hope this helps
>> Jay
>>[/color]
>
>
>
> "Jay B. Harlow [MVP - Outlook]" wrote:
>[color=green]
>> Darren,
>> First "'Select isn't Broken!" :-)
>>
>> http://www.pragmaticprogrammer.com/p...rule_list.html
>>
>>[...]
>>
>> Hope this helps
>> Jay[/color]
>
> Jay,
> Thanks it did help!
>
> After going through and debugging it and looking at the actual characters
> within the string, I found that TRIM wasn't eliminating chr(0) from the
> string. I find it extremely frustrating that something simple like
> removal
> of an unused character is so hard... SO I wrote a real 'TRIM' function.
> I'll probably modify it more over time, but I'm in the middle of a
> project;
> again thanks.
>
>
> -------------------------------
> 'Use this function to remove chr(0) and chr(32) at beginning and end of
> string
> ' Function takes 1 input of string type and outputs string type, without
> excessive characters
> Public Function CleanUpString(ByVal strIn As String) As String
> Dim nStartPos As Integer, nEndPos As Integer 'For finding beginning and
> end of string
> Dim nLen As Integer 'Length of string
>
> CleanUpString = vbNullString 'Start with an empty string
>
> If strIn.Length = 0 Then Exit Function 'Dummy check
>
> nStartPos = 0 'Starting position is at the beginning of the string
>
> 'Find the actual beginning of the string
> Do Until (Not strIn.ToCharArray(nStartPos, 1) = Chr(0) And Not
> strIn.ToCharArray(nStartPos, 1) = Chr(32))
> nStartPos += 1 'iterate forward
> If nStartPos >= Len(strIn) Then Exit Do 'Exit if the whole string
> is
> chr(0) or chr(32)
> Loop
>
> If (nStartPos >= Len(strIn)) Then Exit Function 'If the whole string is
> nothing but chr(0) and chr(32) then return "" (a 'real' empty string)
>
> nEndPos = (Len(strIn) - 1) 'Set the ending position at the end of
> string
>
> 'Find the actual end of the string
> Do Until (Not strIn.ToCharArray(nEndPos, 1) = Chr(0) And Not
> strIn.ToCharArray(nEndPos, 1) = Chr(32))
> nEndPos -= 1 'iterate backward
> If nEndPos <= 0 Then Exit Do
> Loop
>
> nLen = Len(strIn) - (((Len(strIn) - 1) - nEndPos) + nStartPos) 'Get the
> length of important characters in the string
>
> CleanUpString = strIn.Substring(nStartPos, nLen) 'Get the "important"
> string for output
>
> End Function
> -------------------------------------
>
> Darren
>[/color]


Herfried K. Wagner [MVP]
Guest
 
Posts: n/a
#8: Nov 21 '05

re: Bug: string.substring.trim = "" (boolean expression issue)


Darren,

"Darren Anderson" <DarrenAnderson@discussions.microsoft.com> schrieb:[color=blue]
> After going through and debugging it and looking at the actual characters
> within the string, I found that TRIM wasn't eliminating chr(0) from the
> string. I find it extremely frustrating that something simple like
> removal
> of an unused character is so hard... SO I wrote a real 'TRIM' function.
> I'll probably modify it more over time, but I'm in the middle of a
> project;
> again thanks.
>
>
> -------------------------------
> 'Use this function to remove chr(0) and chr(32) at beginning and end of
> string
> ' Function takes 1 input of string type and outputs string type, without
> excessive characters[/color]

Notice that 'Trim' is overloaded:

\\\
Dim t As String = _
s.Trim(New Char() {ControlChars.NullChar, ChrW(&H5)})
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Jay B. Harlow [MVP - Outlook]
Guest
 
Posts: n/a
#9: Nov 21 '05

re: Bug: string.substring.trim = "" (boolean expression issue)


Herfried,
ChrW(&H5)?

Wouldn't that be either ChrW(32) or ChrW(&H20)?

:-)

Jay

"Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
news:%23LgiavdGFHA.1740@TK2MSFTNGP09.phx.gbl...[color=blue]
> Darren,
>
> "Darren Anderson" <DarrenAnderson@discussions.microsoft.com> schrieb:[color=green]
>> After going through and debugging it and looking at the actual characters
>> within the string, I found that TRIM wasn't eliminating chr(0) from the
>> string. I find it extremely frustrating that something simple like
>> removal
>> of an unused character is so hard... SO I wrote a real 'TRIM' function.
>> I'll probably modify it more over time, but I'm in the middle of a
>> project;
>> again thanks.
>>
>>
>> -------------------------------
>> 'Use this function to remove chr(0) and chr(32) at beginning and end of
>> string
>> ' Function takes 1 input of string type and outputs string type, without
>> excessive characters[/color]
>
> Notice that 'Trim' is overloaded:
>
> \\\
> Dim t As String = _
> s.Trim(New Char() {ControlChars.NullChar, ChrW(&H5)})
> ///
>
> --
> M S Herfried K. Wagner
> M V P <URL:http://dotnet.mvps.org/>
> V B <URL:http://dotnet.mvps.org/dotnet/faqs/>[/color]


Herfried K. Wagner [MVP]
Guest
 
Posts: n/a
#10: Nov 21 '05

re: Bug: string.substring.trim = "" (boolean expression issue)


Jay,

"Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP@msn.com> schrieb:[color=blue]
> ChrW(&H5)?
>
> Wouldn't that be either ChrW(32) or ChrW(&H20)?
>
> :-)[/color]

Ooops... :-).

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Phill. W
Guest
 
Posts: n/a
#11: Nov 21 '05

re: Bug: string.substring.trim = "" (boolean expression issue)


"Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP@msn.com> wrote in message
news:ervsVCcGFHA.588@TK2MSFTNGP15.phx.gbl...
.. . .[color=blue][color=green]
> > Trim() will only remove spaces.[/color]
> Interesting, according to MSDN String.Trim() will remove whitespace
> characters, it suggests it is using Char.IsWhiteSpace to determine what is[/color]
a[color=blue]
> whitespace character![/color]

Dang! Our Friends in Redmond snuck /another/ one past me ...

Ah well; not the first time; won't be the last ;-)

Regards,
Phill W.


_AnonCoward
Guest
 
Posts: n/a
#12: Nov 21 '05

re: Bug: string.substring.trim = "" (boolean expression issue)



"Darren Anderson" <DarrenAnderson@discussions.microsoft.com> wrote in
message news:963E2040-E02A-4880-BC3B-8395BB25E2A4@microsoft.com...
: I have a function that I've tried using in an if then statement and
I've
: found that no matter how much reworking I do with the code, the
expected
: result is incorrect.
:
: the code:
:
: If Not (strIn.Substring(410, 10).Trim = "") Then
: 'Something processed
: Else
: 'Something processed
: End If
:
: The string.substring(410,10).trim is equivelent to an emptry string,
e.g.
: "", when it comes in. So the expected result is supposed to be TRUE
when you
: use (strIn.Substring(410, 10).Trim = ""), and I use the NOT keyword
which
: negates the true to a false (or vice versa). I can not get this DAMN
: function to give me the proper boolean expression, is this a LOGIC
bug...
:
: I've tried using string.substring.trim = space(?) and I've tried
: string.substring.trim <> "" and none of these will yield the correct
boolean
: value in an If...Then context


I don't know if this is what you are looking for, but an old vb trick
for testing for empty strings was to test the length (its faster as
opposed to testing the contents two strings). Try this...

If Not (strIn.Substring(410, 10).Trim.Length = 0) Then
DoThis
Else
DoThat
End If

Ralf


Closed Thread


Similar Visual Basic .NET bytes