Home All Groups Group Topic Archive Search About
Author
25 Mar 2005 2:31 PM
Chris Calzaretta
Hello Everybody,
Question

instr function will give you the first instance of the finding
so EX:
so your string looks like

string1 = "testing>This is > just a test > testtesttest"
  instr(string1,">")

so instr will give me a 8 as a return item
i need to get the last instance


Easy way to do this.

Thanks
Chris

Author
25 Mar 2005 2:43 PM
Richard Wilde
Something like this

Untested
Len(mystring) - InStr(StrReverse(mystring), ">")

Richard

Show quoteHide quote
"Chris Calzaretta" <ccalzare***@hotmail.com> wrote in message
news:O9fqPdUMFHA.4092@tk2msftngp13.phx.gbl...
> Hello Everybody,
> Question
>
> instr function will give you the first instance of the finding
> so EX:
> so your string looks like
>
> string1 = "testing>This is > just a test > testtesttest"
>  instr(string1,">")
>
> so instr will give me a 8 as a return item
> i need to get the last instance
>
>
> Easy way to do this.
>
> Thanks
> Chris
>
Author
25 Mar 2005 2:43 PM
Jerry H.
Super easy!

Change "Instr" function to "InStrRev " function, which gets the
position, starting from the right side of the string.

Show quoteHide quote
:)
Author
25 Mar 2005 4:08 PM
Cor Ligthert
Chris,

When you want to use the Zero indexer ( I assume you don't want it however
to get the answers complete than there is instead of the InStringRev as well
the instruction). Don't expect any advantage from that, than that you are
using the Zero as starting indexer instead of that you get back when it is
on the first position a 1.

dim pos as integer = String1.lastindexof(">")
it gives a -1 when that is not in it.

I hope this helps,

Cor
Author
27 Mar 2005 7:24 PM
Zanna
"Cor Ligthert" <notmyfirstn***@planet.nl> ha scritto nel messaggio
news:eSLhfTVMFHA.3852@tk2msftngp13.phx.gbl...


> dim pos as integer = String1.lastindexof(">")

Finally someone that uses the right .Net way instead of the
old-fashoned-VB6-retro-compatibility functions, that aren't OOP and aren't
so fast as native ones.


--
Reporting tool: http://www.neodatatype.net
Author
28 Mar 2005 7:14 AM
Cor Ligthert
Zanna,
>
>> dim pos as integer = String1.lastindexof(">")
>
> Finally someone that uses the right .Net way instead of the
> old-fashoned-VB6-retro-compatibility functions, that aren't OOP and aren't
> so fast as native ones.
>
>
It is nice to wright that I am right, however I did not write this at all,
because it is not true.

This is what I wrote (compressed)

>>Don't expect any advantage from that, than that you are using the 0 as
>>starting
>>indexer instead of the 1.

Cor
Author
28 Mar 2005 12:42 PM
Zanna
"Cor Ligthert" <notmyfirstn***@planet.nl> ha scritto nel messaggio
news:u8C9PX2MFHA.3392@TK2MSFTNGP10.phx.gbl...


> It is nice to wright that I am right, however I did not write this at all,
> because it is not true.

???

You wrote the post or not? :)

> This is what I wrote (compressed)

> >>Don't expect any advantage from that, than that you are using the 0 as
> >>starting
> >>indexer instead of the 1.

Not just this, I can read :)

And sure InStr is sloooower than String.IndexOf, because calls wrapper
functions from Microsoft.VisualBasic namespace.

But my point was on the way you use VB.Net and not on the speed.

--
Reporting tool: http://www.neodatatype.net
Author
28 Mar 2005 1:04 PM
Cor Ligthert
Zanna,

>
> And sure InStr is sloooower than String.IndexOf, because calls wrapper
> functions from Microsoft.VisualBasic namespace.
>
It is twice as fast as indexof, we tested that in this newsgroup about one
and an half year ago.

Cor
Author
28 Mar 2005 1:33 PM
JD
Do you have the test code that was used?

Show quoteHide quote
"Cor Ligthert" <notmyfirstn***@planet.nl> wrote in message
news:%23ugrva5MFHA.2656@TK2MSFTNGP10.phx.gbl...
> Zanna,
>
> >
> > And sure InStr is sloooower than String.IndexOf, because calls wrapper
> > functions from Microsoft.VisualBasic namespace.
> >
> It is twice as fast as indexof, we tested that in this newsgroup about one
> and an half year ago.
>
> Cor
>
>
Author
28 Mar 2005 2:57 PM
JD
In this example, for the InStr function, you don't specify the compare
method (binary or text). What was your "Option Compare" setting? The default
on my project was binary.

When InStr uses binary compare it does not take in effect culture info at
all. The String.IndexOf always uses text and culture info on comparisons. If
you were comparing both methods with InStr using binary, the test wasn't
quite fair or accurate.

When I changed InStr to use text compare, both InStr and String.IndexOf
comparisons were roughly the same with InStr performing a little bit worse
some of the time, at least on my machine.




"Cor Ligthert" <notmyfirstn***@planet.nl> wrote in message
news:eP8Or65MFHA.3228@TK2MSFTNGP12.phx.gbl...
> JD,
>
> See this thread
>
http://groups-beta.google.com/group/microsoft.public.dotnet.languages.vb/browse_frm/thread/d9a3a1e441c5ef92/315c33cc87237dbf#315c33cc87237dbf
Show quoteHide quote
>
> However there is more.
>
> Cor
>
>
Author
28 Mar 2005 6:24 PM
Cor Ligthert
JD,

I am not asking what is the fairest, the only question is. Can you let the
indexof go as fast as the instr can go. The rest is a non isue.

It is in my opinion quiet simple, the indexof has a lot of more overloads
than the Instr what is a quiet simple method compared with the indexof.

In addition, although that it is twice as slow do I use the indexof by the
way, just because that zero indexer.

Cor
Author
28 Mar 2005 11:45 PM
JD
Hey Cor,

Question. What is the correct InStr answer for the following code?

        Dim str1 As String = "Æpple"
        Thread.CurrentThread.CurrentCulture = New CultureInfo("en-US")
        Console.WriteLine(InStr(str1, "A")) 'this assumes binary compare is
default option in the project settings
        Console.WriteLine(InStr(str1, "A", CompareMethod.Text))

Thanks
Jeff

Show quoteHide quote
"Cor Ligthert" <notmyfirstn***@planet.nl> wrote in message
news:uTj26N8MFHA.244@TK2MSFTNGP12.phx.gbl...
> JD,
>
> I am not asking what is the fairest, the only question is. Can you let the
> indexof go as fast as the instr can go. The rest is a non isue.
>
> It is in my opinion quiet simple, the indexof has a lot of more overloads
> than the Instr what is a quiet simple method compared with the indexof.
>
> In addition, although that it is twice as slow do I use the indexof by the
> way, just because that zero indexer.
>
> Cor
>
>
Author
29 Mar 2005 7:02 AM
Cor Ligthert
JD,

I interpretted your message wrong sorry.

This is not only tested by me, there where more and I can tell you that at
leaset I was very suprissed. I use forever indexof and never 1 starting
indexer based methods.

The indexof becomes much faster than the instr when it is about a character
and you tell that it is a character in the parameters. Probably is the
reason that Instr is a very simple method with no overloads (it has
defaults), while the indexof is a very overloaded method and has therefore
more to evaluate.

That the Instr is faster than indexof is not a reason that other Microsoft
VisualBasic namespace methods are faster as well. I have seen some which are
very slow comparing with other commands.

Cor
Author
28 Mar 2005 6:38 PM
Zanna
"Cor Ligthert" <notmyfirstn***@planet.nl> ha scritto nel messaggio
news:#ugrva5MFHA.2656@TK2MSFTNGP10.phx.gbl...

> > And sure InStr is sloooower than String.IndexOf, because calls wrapper
> > functions from Microsoft.VisualBasic namespace.
> >
> It is twice as fast as indexof,
> we tested that in this newsgroup about one
> and an half year ago.

I don't understand your test, btw InStr and IndexOf are quite simple
functions (you can test their code with Reflector) so the speed in same
conditions should be very close (I really don't believe the twice-story).

But, as I sayd, the point is the way you write the code!

As Martin Flower sayd "Any fool can write code that a computer can
understand. Good programmers write code that humans can understand".
This means also that you shoud not use bad practices in your code, and using
an old deprecated VB6-compliant non-OOP function I think can be put with the
bad practices.

Also the InStr is an example, but the same can be said for Mid$, Left$ and
so on.

Take VB.Net for the tool it is, and, it's sure, it's NOT VB7

--
Reporting tool: http://www.neodatatype.net
Author
29 Mar 2005 6:57 AM
Cor Ligthert
Zanna,

> But, as I sayd, the point is the way you write the code!

That is why my first sample in this thread was stated on.
>
> As Martin Flower sayd "Any fool can write code that a computer can
> understand. Good programmers write code that humans can understand".

Martin "wrote" it, in my memory this sentense exist much longer it is one of
the basics of a good readable program.

> This means also that you shoud not use bad practices in your code, and
> using
> an old deprecated VB6-compliant non-OOP function I think can be put with
> the
> bad practices.

This has nothing to do with the previous sentence. "Instr" has as much to do
with OOP as "indexof" has to do with OOP; Nothing,  it are string evaluating
methods..

>
> Also the InStr is an example, but the same can be said for Mid$, Left$ and
> so on.
>
That is true,

> Take VB.Net for the tool it is, and, it's sure, it's NOT VB7
>
No however those methods you mention can be used in C# or whatever other Net
language as well. So why would I use them in C# and not in VBNet. They are
an integrated part of the distributed framework. (Not the keywords as the
Static value, those belong to VBNet).

Cor
Author
29 Mar 2005 7:11 AM
Zanna
"Cor Ligthert" <notmyfirstn***@planet.nl> ha scritto nel messaggio
news:eztmuyCNFHA.1392@TK2MSFTNGP10.phx.gbl...


> No however those methods you mention can be used in C# or whatever other
Net
> language as well. So why would I use them in C# and not in VBNet. They are
> an integrated part of the distributed framework. (Not the keywords as the
> Static value, those belong to VBNet).

Well, I think you misunderstand what I'm sayng :)
I told "use .IndexOf: IndexOf is better than InStr".

The same you say in the phrase above.


--
Reporting tool: http://www.neodatatype.net
Author
29 Mar 2005 8:48 AM
Cor Ligthert
>
> Well, I think you misunderstand what I'm sayng :)
> I told "use .IndexOf: IndexOf is better than InStr".
>
> The same you say in the phrase above.
>
I did nowhere write it is "better", when you see Hefried samples, than he is
forever using Mid, Right, Left in that case I would find it very incosequent
to use indexof.

Cor
Author
29 Mar 2005 5:41 PM
Zanna
"Cor Ligthert" <notmyfirstn***@planet.nl> ha scritto nel messaggio
news:#OQOnwDNFHA.1948@TK2MSFTNGP14.phx.gbl...


> I did nowhere write it is "better", when you see Hefried samples, than he
is
> forever using Mid, Right, Left in that case I would find it very
incosequent
> to use indexof.

:)

I don't care about bad-coded-samples ;)

--
Reporting tool: http://www.neodatatype.net
Author
25 Mar 2005 5:12 PM
Herfried K. Wagner [MVP]
Chris,

"Chris Calzaretta" <ccalzare***@hotmail.com> schrieb:
> instr function will give you the first instance of the finding
> so EX:
> so your string looks like
>
> string1 = "testing>This is > just a test > testtesttest"
>  instr(string1,">")
>
> so instr will give me a 8 as a return item
> i need to get the last instance

'Microsoft.VisualBasic.Strings.InStrRev'.

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