Substring question

F

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
 
M

Marina Levit [MVP]

In this particular case, I would use:

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

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
 
H

Herfried K. Wagner [MVP]

Marina Levit said:
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.
 
O

Oenone

fniles said:
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.
 
J

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


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

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


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

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


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

Herfried K. Wagner [MVP]

Jay B. Harlow said:
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.
 
J

Jay B. Harlow [MVP - Outlook]

Herfried,
| U+FEFF is "deprecated" and U+2060 should be used instead because U+FEFF is
a
| typical UTF-BOM too.
I was going to say it looked like a BOM, odd it is (was) also a character.


| > 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.
I try to use StringComparison.InvariantCulture & StringComparison.Ordinal
where appropriate:

http://blogs.msdn.com/bclteam/archive/2005/06/01/424012.aspx
http://msdn.microsoft.com/netframew...ibrary/en-us/dndotnet/html/StringsinNET20.asp

Although sometimes I forget to use them initially, then remember on
subsequent code reviews... FWIW: I should thank you! Looking for where I
used the option I noticed a handful of places where I should have used
StringComparison.Ordinal instead of StringComparison.InvariantCulture! I'll
need to change them sometime today...

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


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

Jay B. Harlow [MVP - Outlook]

I would be very careful with statements that suggest you avoid
String.IsNullOrEmpty specifically!

As I would find it *extremely* odd indeed if it (String.IsNullOrEmpty)
specifically caused a JIT bug to manifest. That *absolutely* *no* other
routine caused this bug to manifest.

Rather I would suspect any routine that is written similar to how
String.IsNullOrEmpty is written when coupled with Bill's sample code would
cause the same JIT bug to manifest itself.

I would find more helpful & even expect statements that suggest you be
caution when using some particular code pattern... (the code pattern that
causes the JIT bug to manifest)...

Especially when every indication suggests it *is* a JIT bug (both Bill &
some of the comments suggest its a JIT bug). It sounds like a JIT bug to me.


Semantics yes, however I can see developers avoiding String.IsNullOrEmpty
only to be smacked hard by the JIT bug!


Thanks for the heads up.

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


| Jay,
|
| > 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/>
|
 
C

Cor Ligthert[MVP]

Hi Fred,

Then at least avoid the IIf as it is something left from very long ago

However, what is for you the vb6 namespace, has that to do with Com or
something.
The microsoft.visualbasic namespace is an integral part of the Framework
version Net 1.0, Net 1.1, Net 2.0, Net 3.0, Net 3.5 and is probably the most
ready part of Net 4.0.

Cor
 
M

Michel Posseth [MCP]

VB6 namespacces ,, huh where ???
I would do this:

Dim str As String
str = sStr.SubString(0,IIf(sStr.Length > 3, 3, sStr.Length))

Instead of IIf you might consider If


Dim str As String
str = sStr.SubString(0,If(sStr.Length > 3, 3, sStr.Length))


regards

Michel
 
C

Cor Ligthert[MVP]

Michel,

I assume that you mean something as

Dim str as string
if sStr.length > 3 then
str = sStr.substring(0,3)
else
str = sStr
end if

This is C# like code and I prefer the above. However, I am almost sure that
we get a message from Herfried which shows that the Mid does here a better
job because that has no problems as the string is to short and in fact I
only can say then that he is right.

Cor
 
J

James Hahn

Using
str = sStr.SubString(0,If(sStr.Length > 3, 3, sStr.Length))
is preferable to
str = sStr.SubString(0,IIf(sStr.Length > 3, 3, sStr.Length))
because the former uses short-circuit evaluation.
 
C

Cor Ligthert[MVP]

James,

I was me not aware anymore of that construction, I am happy Michel did not
see it.

:)

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Is this the fastest way 4
Late Binding Question 4
Upper Case Woes 2
Help with messed-up code!!! 2
Error 91: Object Variable Not Set 3
Any ideas... Doesn't work... 1
unwanted copy 12
Can you fix this?? vbreadonly 7

Top