InStr

  • Thread starter Thread starter Chris Calzaretta
  • Start date Start date
C

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
 
Something like this

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

Richard
 
Super easy!

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

:)
 
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
 
Chris,

Chris Calzaretta said:
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'.
 
"Cor Ligthert" <[email protected]> ha scritto nel messaggio

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.
 
Zanna,
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)

Cor
 
"Cor Ligthert" <[email protected]> ha scritto nel messaggio

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)

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.
 
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
 
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.
 
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
 
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
 
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
 
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
 
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
 
"Cor Ligthert" <[email protected]> ha scritto nel messaggio

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.
 
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
 
"Cor Ligthert" <[email protected]> ha scritto nel messaggio

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 ;)
 
Back
Top