473,701 Members | 2,582 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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.Substrin g(410, 10).Trim = "") Then
'Something processed
Else
'Something processed
End If

The string.substrin g(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.Substrin g(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.substrin g.trim = space(?) and I've tried
string.substrin g.trim <> "" and none of these will yield the correct boolean
value in an If...Then context
Nov 21 '05 #1
11 5349
"Darren Anderson" <Da************ @discussions.mi crosoft.com> schrieb:
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.Substrin g(410, 10).Trim = "") Then
'Something processed
Else
'Something processed
End If

The string.substrin g(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.Substrin g(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...


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/>

Nov 21 '05 #2
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?

The string.substrin g(410,10).trim is equivalent to an empty string, e.g.
"", If you assign string.substrin g(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.Substrin g(410, 10).Trim() = ""), "not
(substring.trim = space)")
Debug.WriteLine (strIn.Substrin g(410, 10).Trim() <> "",
"substring. trim <> space")
Debug.WriteLine (strIn.Substrin g(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" <Da************ @discussions.mi crosoft.com> wrote in
message news:96******** *************** ***********@mic rosoft.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.Substrin g(410, 10).Trim = "") Then
'Something processed
Else
'Something processed
End If

The string.substrin g(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.Substrin g(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.substrin g.trim = space(?) and I've tried
string.substrin g.trim <> "" and none of these will yield the correct
boolean
value in an If...Then context

Nov 21 '05 #3
"Darren Anderson" <Da************ @discussions.mi crosoft.com> wrote in
message news:96******** *************** ***********@mic rosoft.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.

If Not (strIn.Substrin g(410, 10).Trim = "") Then


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.
Nov 21 '05 #4
Phill,
Are you sure the substring contains /exactly/ 10 /spaces/?
Could any of them be Null or other whitespace characters?
Trim() will only remove spaces. Interesting, according to MSDN String.Trim() will remove whitespace
characters, it suggests it is using Char.IsWhiteSpa ce 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.IsWhiteSpa ce 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.MinVa lue) To AscW(Char.MaxVa lue)
Dim ch As Char = ChrW(charCode)
If Char.IsWhiteSpa ce(ch) Then
sb.Append(ch)
Debug.WriteLine (String.Format( format, charCode),
"IsWhiteSpa ce")
End If
Next

Dim str As String = sb.ToString().T rim()
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(Cha r())

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().T rim(whitespace)

Hope this helps
Jay
"Phill. W" <P.A.Ward@o-p-e-n-.-a-c-.-u-k> wrote in message
news:cv******** **@yarrow.open. ac.uk... "Darren Anderson" <Da************ @discussions.mi crosoft.com> wrote in
message news:96******** *************** ***********@mic rosoft.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.

If Not (strIn.Substrin g(410, 10).Trim = "") Then


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.

Nov 21 '05 #5


"Jay B. Harlow [MVP - Outlook]" wrote:
Darren,
First "'Select isn't Broken!" :-)

http://www.pragmaticprogrammer.com/p...rule_list.html
[...]
What do you see if you use your original string instead of "New String(" "c,
450)" for strIn?

Hope this helps
Jay

"Jay B. Harlow [MVP - Outlook]" wrote:
Darren,
First "'Select isn't Broken!" :-)

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

[...]

Hope this helps
Jay


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(B yVal 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.ToCharArr ay(nStartPos, 1) = Chr(0) And Not
strIn.ToCharArr ay(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.ToCharArr ay(nEndPos, 1) = Chr(0) And Not
strIn.ToCharArr ay(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

Nov 21 '05 #6
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(B yVal 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.
Do While (strIn.Chars(nS tartPos) = Chr(0) OrElse strIn.Chars(nSt artPos)
= Chr(32))
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" <Da************ @discussions.mi crosoft.com> wrote in
message news:2B******** *************** ***********@mic rosoft.com...

"Jay B. Harlow [MVP - Outlook]" wrote:
Darren,
First "'Select isn't Broken!" :-)

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

[...]

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

Hope this helps
Jay


"Jay B. Harlow [MVP - Outlook]" wrote:
Darren,
First "'Select isn't Broken!" :-)

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

[...]

Hope this helps
Jay


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(B yVal 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.ToCharArr ay(nStartPos, 1) = Chr(0) And Not
strIn.ToCharArr ay(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.ToCharArr ay(nEndPos, 1) = Chr(0) And Not
strIn.ToCharArr ay(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

Nov 21 '05 #7
Darren,

"Darren Anderson" <Da************ @discussions.mi crosoft.com> schrieb:
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


Notice that 'Trim' is overloaded:

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

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

Nov 21 '05 #8
Herfried,
ChrW(&H5)?

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

:-)

Jay

"Herfried K. Wagner [MVP]" <hi************ ***@gmx.at> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
Darren,

"Darren Anderson" <Da************ @discussions.mi crosoft.com> schrieb:
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


Notice that 'Trim' is overloaded:

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

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

Nov 21 '05 #9
Jay,

"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> schrieb:
ChrW(&H5)?

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

:-)


Ooops... :-).

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1250
by: turar | last post by:
Hi, I wonder if anyone knows of a free Bug/Issue/Trouble-ticket tracking ASP application. Thanks...
9
1873
by: Stefan Mueller | last post by:
I'd like to set a variable called 'FocusIsOn' if a button got the focus. Because my button is dynamically created I do it like xelement = document.createElement("input") xelement.type = "button" element.onfocus = function() {FocusIsOn = "Button_1"; alert(FocusIsOn)} That works great. If the button gets the focus, I got a message box saying 'Button_1'. However, if I set the variable 'FocusIsOn' dynamically (using a variable
32
2686
by: Indrid Colt | last post by:
Thank's for your help ! Indrid
9
7133
by: rsine | last post by:
I have developed a program that sends a command through the serial port to our business system and then reads from the buffer looking for a number. Everything worked great on my WinXP system, but when I tried the program on the Win98 system it will be running on, I get the following error: Cast from string "2076719" to type 'Long' is not valid I am not sure why I only get this error on the Win98 system or how to go about correcting...
3
2008
by: hazz | last post by:
I get an InvalidCastException "Cast from string "6<5" to type 'Boolean is not valid. for If (CInt(objBuyInfo.Budget) & " " & arrRuleList(i).operator & " " & CInt(arrRuleList(i).LeadMemberValue)) Then where arrRuleList(i).operator = ">"
4
13771
by: dc15 | last post by:
For an intro to VB project I have to write a program which takes an amount of Miles, Yards, and Inches.....and converts it to metric (KM, M, and CM) when all values are entered to the input text boxes it works fine, but if there is a number missing I get the following error: An unhandled exception of type 'System.InvalidCastException' occurred in microsoft.visualbasic.dll Additional information: Cast from string "" to type 'Double' is...
4
1975
by: Wilson | last post by:
Hello, How can i eval a string expression like String abc = ??"dt.Rows.Columns.ToString() + dt.Rows.Columns.ToString()" Thanks Wilson
2
2159
by: parul.prasad | last post by:
How can we evaluate a string expression in if statement Linux C/C++
4
1635
by: moogyd | last post by:
Hi, (Off-topic) I am looking to put an open-source bug/issue tracking system in place for our current project (eventually expanded for all projects), and would appreciate any experiences/comments/suggestions. Note the project is encompasses embedded hardware (ASIC plus firmware) plus application software.
0
8736
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9229
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9083
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8934
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7824
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6571
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4410
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4662
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3102
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.