Home All Groups Group Topic Archive Search About
Author
2 May 2006 3:21 PM
fniles
In VB 6 I used to do  like the following:
if left(str,3) = "abc" then

In VB.NET when I do the following
sStr.Substring(0, 3), and the sStr has fewer than 3 characters, it will give
me an error.

For ex: sStr = "a", if I do sStr.Substring(0, 3), it will give me an error.
So, I was forced to do

if len(sStr) >= 3 then
if sStr.Substring(0, 3)
:
Is there an easier way to to it besides the above code ?

Thanks

Author
2 May 2006 3:37 PM
Marina Levit [MVP]
In this particular case, I would use:

If str.StartsWith("abc") Then
....

Show quoteHide quote
"fniles" <fni***@pfmail.com> wrote in message
news:ezjKGxfbGHA.3840@TK2MSFTNGP04.phx.gbl...
> In VB 6 I used to do  like the following:
> if left(str,3) = "abc" then
>
> In VB.NET when I do the following
> sStr.Substring(0, 3), and the sStr has fewer than 3 characters, it will
> give me an error.
>
> For ex: sStr = "a", if I do sStr.Substring(0, 3), it will give me an
> error. So, I was forced to do
>
> if len(sStr) >= 3 then
> if sStr.Substring(0, 3)
> :
> Is there an easier way to to it besides the above code ?
>
> Thanks
>
>
Author
2 May 2006 4:03 PM
Herfried K. Wagner [MVP]
"Marina Levit [MVP]" <someone@nospam.com> schrieb:
> In this particular case, I would use:
>
> If str.StartsWith("abc") Then

Note that is is not 100 % equivalent:

\\\
MsgBox("Foo".StartsWith(ChrW(&HFEFF) & "Foo")
///

will return 'True' on a 'de-DE' and 'en-US' system in .NET 2.0, for example.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
3 May 2006 12:49 PM
Jay B. Harlow [MVP - Outlook]
Jay,
What is character ChrW(&HFEFF) anyway?

You could always use:

        MsgBox("Foo".StartsWith(ChrW(&HFEFF) & "Foo",
StringComparison.OrdinalIgnoreCase))

FWIW: I tried StringComparison.InvariantCulture above & it also returns
true.

In VB6 did you try Option Compare Text or Option Compare Binary?

Depending on what ChrW(&HFEFF) is, the results you see may be the more
correct (between VB6 & .NET).

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


Show quoteHide quote
"Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
news:OMu$lHgbGHA.4900@TK2MSFTNGP02.phx.gbl...
| "Marina Levit [MVP]" <someone@nospam.com> schrieb:
| > In this particular case, I would use:
| >
| > If str.StartsWith("abc") Then
|
| Note that is is not 100 % equivalent:
|
| \\\
| MsgBox("Foo".StartsWith(ChrW(&HFEFF) & "Foo")
| ///
|
| will return 'True' on a 'de-DE' and 'en-US' system in .NET 2.0, for
example.
|
| --
| M S   Herfried K. Wagner
| M V P  <URL:http://dotnet.mvps.org/>
| V B   <URL:http://classicvb.org/petition/>
|
Author
3 May 2006 2:13 PM
Herfried K. Wagner [MVP]
"Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_***@tsbradley.net> schrieb:
> What is character ChrW(&HFEFF) anyway?

U+FEFF "ZERO WIDTH NON-BREAKING SPACE"
U+2060 "WORD JOINER"

U+FEFF is "deprecated" and U+2060 should be used instead because U+FEFF is a
typical UTF-BOM too.  However, the behavior is the same for the U+2060
character.  The problem I am seeing is that the behavior changed from .NET
1.* to .NET 2.0.

> You could always use:
>
>        MsgBox("Foo".StartsWith(ChrW(&HFEFF) & "Foo",
> StringComparison.OrdinalIgnoreCase))
>
> FWIW: I tried StringComparison.InvariantCulture above & it also returns
> true.

Sure, I know!  I have read a lot of code but I have rarely seen anybody
specifying this option, even if it should have been specified.

> In VB6 did you try Option Compare Text or Option Compare Binary?

I tried it in VB.NET.  'Option Compare Text' performs a culture-specific
comparison while 'Option Compare Binary' performs an ordinal comparison.
With 'Option Compare Text' the expression '"Foo" = ChrW(&HFEFF) & "Foo"'
will return 'True', which isn't the case if 'Option Compare Binary' is used.

> Depending on what ChrW(&HFEFF) is, the results you see may be the more
> correct (between VB6 & .NET).

I didn't compare VB6 to VB.NET in this particular case before posting.  But
interestingly '"Foo" = ChrW(&HFEFF) & "Foo"' evaluates to 'True' with
'Option Compare Text' specified in VB6 while it evaluates to 'False' with
'Option Compare Binary'.  So the behavior is not really new, but really
less-known.


--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
2 May 2006 3:40 PM
Cor Ligthert [MVP]
Fniles,

Why do you not use that same instructions as in VB6 as they are there if you
are used to those. They are legal (as well in the future) in VBNet.

The most of the VB functions have something extra, like to overcome the
problem as you wrote.

Cor


Show quoteHide quote
"fniles" <fni***@pfmail.com> schreef in bericht
news:ezjKGxfbGHA.3840@TK2MSFTNGP04.phx.gbl...
> In VB 6 I used to do  like the following:
> if left(str,3) = "abc" then
>
> In VB.NET when I do the following
> sStr.Substring(0, 3), and the sStr has fewer than 3 characters, it will
> give me an error.
>
> For ex: sStr = "a", if I do sStr.Substring(0, 3), it will give me an
> error. So, I was forced to do
>
> if len(sStr) >= 3 then
> if sStr.Substring(0, 3)
> :
> Is there an easier way to to it besides the above code ?
>
> Thanks
>
>
Author
2 May 2006 5:49 PM
Oenone
fniles wrote:
> Is there an easier way to to it besides the above code ?

Yep, just keep using the Left() function.

There are lots of VB6-style string functions that I prefer to use over the
methods performed on strings themselves. One reason for this is that if you
string is uninitialised, you'll get a Null Reference exception if you try to
call a method on it. The VB6 string commands handle this without any
problem. This VB6-style code:

\\\
    If Len(myString) > 0 Then
    [...]
    End If
///

....would otherwise turn into the much less readable:

\\\
    If myString IsNot Nothing AndAlso myString.Length > 0 Then
    [...]
    End If
///

The first piece of code is much more concise.

--

(O)enone
Author
3 May 2006 12:36 PM
Jay B. Harlow [MVP - Outlook]
Oenone,
A .NET 2.0 alternative to:
| \\\
|    If myString IsNot Nothing AndAlso myString.Length > 0 Then
|    [...]
|    End If
| ///

Would be String.IsNullOrEmpty

    If String.IsNullOrEmpty(myString) Then
    [...]
    End If

http://msdn2.microsoft.com/en-us/library/490acw3e(vs.80).aspx

I agree, your first is more concise; I find IsNullOrEmpty to be more
"obvious", while "myString IsNot Nothing AndAlso myString.Length > 0" or
"myString IsNot Nothing AndAlso myString <> "" " are overly wordy.

By obvious I mean the code states more clearly what its doing. Its checking
the string variable to see if its null or empty. The Len function simply
says I am requesting the length of the parameter; I would use the Len or
String.Length property when I needed to know what the length actually was...
FWIW: I rarely use the length to check for empty, although I'm not opposed
to using length to check for empty.

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


Show quoteHide quote
"Oenone" <oen***@nowhere.com> wrote in message
news:mYM5g.1069$LU3.693@newsfe2-gui.ntli.net...
| fniles wrote:
| > Is there an easier way to to it besides the above code ?
|
| Yep, just keep using the Left() function.
|
| There are lots of VB6-style string functions that I prefer to use over the
| methods performed on strings themselves. One reason for this is that if
you
| string is uninitialised, you'll get a Null Reference exception if you try
to
| call a method on it. The VB6 string commands handle this without any
| problem. This VB6-style code:
|
| \\\
|    If Len(myString) > 0 Then
|    [...]
|    End If
| ///
|
| ...would otherwise turn into the much less readable:
|
| \\\
|    If myString IsNot Nothing AndAlso myString.Length > 0 Then
|    [...]
|    End If
| ///
|
| The first piece of code is much more concise.
|
| --
|
| (O)enone
|
|
Author
3 May 2006 2:15 PM
Herfried K. Wagner [MVP]
Jay,

"Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_***@tsbradley.net> schrieb:
> A .NET 2.0 alternative to:
> | \\\
> |    If myString IsNot Nothing AndAlso myString.Length > 0 Then
> |    [...]
> |    End If
> | ///
>
> Would be String.IsNullOrEmpty

True, but don't forget to check out
<URL:http://msmvps.com/blogs/bill/archive/2006/04/04/89234.aspx>.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
3 May 2006 12:38 PM
Jay B. Harlow [MVP - Outlook]
Fniles,
As the others suggest you can continue to use the Left function in .NET.

One caveat, the Control.Left hides VB's Left function (in a form for
example), normally I introduce a namespace alias & use VB.Left when I
need/want to use the function.

Something
Imports VB = Microsoft.VisualBasic

    If VB.Left(str,3) = "abc" Then

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


Show quoteHide quote
"fniles" <fni***@pfmail.com> wrote in message
news:ezjKGxfbGHA.3840@TK2MSFTNGP04.phx.gbl...
| In VB 6 I used to do  like the following:
| if left(str,3) = "abc" then
|
| In VB.NET when I do the following
| sStr.Substring(0, 3), and the sStr has fewer than 3 characters, it will
give
| me an error.
|
| For ex: sStr = "a", if I do sStr.Substring(0, 3), it will give me an
error.
| So, I was forced to do
|
| if len(sStr) >= 3 then
| if sStr.Substring(0, 3)
| :
| Is there an easier way to to it besides the above code ?
|
| Thanks
|
|