473,322 Members | 1,501 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Substring question

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
May 2 '06 #1
11 2186
In this particular case, I would use:

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

"fniles" <fn****@pfmail.com> wrote in message
news:ez**************@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

May 2 '06 #2
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
"fniles" <fn****@pfmail.com> schreef in bericht
news:ez**************@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

May 2 '06 #3
"Marina Levit [MVP]" <so*****@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/>

May 2 '06 #4
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
May 2 '06 #5
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/lib...3e(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
"Oenone" <oe****@nowhere.com> wrote in message
news:mY****************@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
|
|
May 3 '06 #6
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
"fniles" <fn****@pfmail.com> wrote in message
news:ez**************@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
|
|
May 3 '06 #7
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
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:OM**************@TK2MSFTNGP02.phx.gbl...
| "Marina Levit [MVP]" <so*****@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/>
|
May 3 '06 #8
"Jay B. Harlow [MVP - Outlook]" <Ja************@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/>

May 3 '06 #9
Jay,

"Jay B. Harlow [MVP - Outlook]" <Ja************@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/>

May 3 '06 #10
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/archiv...01/424012.aspx
http://msdn.microsoft.com/netframewo...ngsinNET20.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
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:uf****************@TK2MSFTNGP02.phx.gbl...
| "Jay B. Harlow [MVP - Outlook]" <Ja************@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/>
|
May 4 '06 #11
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
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:Oo**************@TK2MSFTNGP04.phx.gbl...
| Jay,
|
| "Jay B. Harlow [MVP - Outlook]" <Ja************@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/>
|
May 4 '06 #12

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

Similar topics

1
by: sysindex | last post by:
I am trying to find a way to dynamically retrieve the substring starting point of an nText field. My query looks something like SELECT ID,Substring(DOCTEXT,0,200) from mytable where DOCTEXT...
0
by: Gazi Mahmud | last post by:
I am trying to find a way to dynamically retrieve the substring starting point for an nText field. My query looks something like SELECT ID,Substring(DOCTEXT,0,200) from mytable where DOCTEXT...
6
by: dreamerbin | last post by:
Hi, I'm having trouble extracting substrings using regular expression. Here is my problem: Want to find the substring that is immediately before a given substring. For example: from "00...
2
by: Mike P | last post by:
If I want to get the last 4 characters of a string is this the right way to do it? int intCardLength = Convert.ToInt32(strCardNumberUnEnc.Length); strCardNumberUnEnc =...
3
by: colinhumber | last post by:
How can I use regular expressions to search for a substring within another substring? For example, if I have the string: "This is * some text <button id="123">Some * text</button>" and I only...
2
by: David Filion | last post by:
Hi, I have a question about substring(), when I run the following query: prepaid=# select substring('15148300', 0, 5); substring ----------- 1514 (1 row)
2
by: mallard134 | last post by:
Could someone please help a newbee vb programmer with a question that is driving me crazy. I am trying to understand a line of code that is supposed to return the domain portion of a valid email...
2
by: Daniel Reardon | last post by:
All, I'm sure that this is an old question, but I'm having problems getting a substring of a passed in variable. I can do other string manipulation with it such as concatonation, but I'm really...
3
by: =?Utf-8?B?anAybXNmdA==?= | last post by:
Two part question: 1. Is Regex more efficient than manually comparing values using Substring? 2. I've never created a Regex expression. How would I use regex to do the equivalent of what I...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.