|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Substring questionIn 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 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 > > "Marina Levit [MVP]" <someone@nospam.com> schrieb: Note that is is not 100 % equivalent:> In this particular case, I would use: > > If str.StartsWith("abc") Then \\\ 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/> 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). -- Show quoteHide quoteHope this helps Jay B. Harlow [MVP - Outlook] ..NET Application Architect, Enthusiast, & Evangelist T.S. Bradley - http://www.tsbradley.net "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/> | "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_***@tsbradley.net> schrieb: U+FEFF "ZERO WIDTH NON-BREAKING SPACE"> What is character ChrW(&HFEFF) anyway? 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: Sure, I know! I have read a lot of code but I have rarely seen anybody > > MsgBox("Foo".StartsWith(ChrW(&HFEFF) & "Foo", > StringComparison.OrdinalIgnoreCase)) > > FWIW: I tried StringComparison.InvariantCulture above & it also returns > true. 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 I didn't compare VB6 to VB.NET in this particular case before posting. But > correct (between VB6 & .NET). 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/> 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 > > 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 Oenone,
A .NET 2.0 alternative to: | \\\ Would be String.IsNullOrEmpty| If myString IsNot Nothing AndAlso myString.Length > 0 Then | [...] | End If | /// 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. -- Show quoteHide quoteHope this helps Jay B. Harlow [MVP - Outlook] ..NET Application Architect, Enthusiast, & Evangelist T.S. Bradley - http://www.tsbradley.net "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 | | Jay,
"Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_***@tsbradley.net> schrieb: True, but don't forget to check out > A .NET 2.0 alternative to: > | \\\ > | If myString IsNot Nothing AndAlso myString.Length > 0 Then > | [...] > | End If > | /// > > Would be String.IsNullOrEmpty <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/> 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 -- Show quoteHide quoteHope this helps Jay B. Harlow [MVP - Outlook] ..NET Application Architect, Enthusiast, & Evangelist T.S. Bradley - http://www.tsbradley.net "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 | |
vb.net, so slow !!!???
General Question About Web Apps, VB/ASP? Support for multiple database Accessing existing Excel Workbook from complete path msmapi32.ocx Appending two data tables How to control Bluetooth? Screen Scraper that does Automated data entry. Debugging DLLs - best practice Application Error: CiceroUIWndFrame |
|||||||||||||||||||||||