InStr() vs InStrRev() What's the difference? Is there a reverse String search tool in vb.net?

InStr() vs InStrRev() What's the difference? Is there a reverse String search tool in vb.net?

WCrihfield
Mentor Mentor
3,345 Views
5 Replies
Message 1 of 6

InStr() vs InStrRev() What's the difference? Is there a reverse String search tool in vb.net?

WCrihfield
Mentor
Mentor

I have a couple of fairly fundamental VB.NET questions that I'm hoping some of you with a long history of VB.NET (not necessarily VBA) coding background can answer.

What is the difference between the InStr() function and the InStrRev() function in VB.NET?

I have read Microsoft's page for both, and it says they are supposed to function differently from each other, in fact opposite/reverse from each other, but they don't seem to work that way.  They both seem do the same exact thing, the same exact way.

 

   It says the InStr() function searches the source input string from left/start to right/end, looking for the target input String, then returns an Integer, which represents the position of where that target String is found within the source String, by counting the characters from the left/start of the source String to where it finds the target String.

This works exactly as it is described.

  And it says the InStrRev() function is supposed to search through the source String to find the target String, but in the opposite direction (or reverse, hence the "Rev" in InStrRev).  It is supposed to start searching and counting the characters from the right/end of the source String, towards the left/end of the source String, until it finds the target String, then return that count as the Integer.

This does not work as described.

Why?

Is this a glitch or error in iLogic, or in Inventor?

Has anyone tried using these outside of Inventor? If so, do they act differently there?

What's the use of one searching from left to right and the other searching from right to left if they both return the same Integer, when the target is obviously not in the same position from either end of the String?

If this is not an error or glitch, then is there another different function built into VB.NET that works the way InStrRev's description sounds like it should work?

Is sure seems like VB.NET should have implemented a function that works the way I'm wanting by now.

Within this iLogic rule:

Dim oSource As String = "ABCDEFGH"
Dim oTarget As String = "G"
MsgBox("InStr(oSource, oTarget) = " & InStr(oSource, oTarget) & vbCrLf & _
"InStrRev(oSource, oTarget) = " & InStrRev(oSource, oTarget))

both functions return 7.

But it seems to me that the InStrRev() function should be returning 2, instead of 7, if it were counding from the right/end.  I want a function that finds the position of the oTarget ("G") within the oSource ("ABCDEFGH"), starting from the right/end, and counting how many characters the oTarget is from that right/end.

This functionality I seek sure seems like an extremely useful tool for an extremely common data type (String) that should have been implemented by now.

I've tried every setting of every option that both of these functions have, but they don't change how they work.

Any explanations, alternate functions, or suggestions welcome.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Accepted solutions (1)
3,346 Views
5 Replies
Replies (5)
Message 2 of 6

bradeneuropeArthur
Mentor
Mentor

The instr is from the start of the string

The instrev is from the end of the string.

Regards,

Arthur Knoors

Autodesk Affiliations & Links:
blue LinkedIn LogoSquare Youtube Logo Isolated on White Background


Autodesk Software:Inventor Professional 2025 | Vault Professional 2024 | Autocad Mechanical 2024
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:
Drawing List!|
Toggle Drawing Sheet!|
Workplane Resize!|
Drawing View Locker!|
Multi Sheet to Mono Sheet!|
Drawing Weld Symbols!|
Drawing View Label Align!|
Open From Balloon!|
Model State Lock!
Posts and Ideas:
My Ideas|
Dimension Component!|
Partlist Export!|
Derive I-properties!|
Vault Prompts Via API!|
Vault Handbook/Manual!|
Drawing Toggle Sheets!|
Vault Defer Update!

! For administrative reasons, please mark a "Solution as solved" when the issue is solved !


 


EESignature

0 Likes
Message 3 of 6

bradeneuropeArthur
Mentor
Mentor

If you search longer strings then it represents different positions i assume. Try it with search "AB"  for example 

Regards,

Arthur Knoors

Autodesk Affiliations & Links:
blue LinkedIn LogoSquare Youtube Logo Isolated on White Background


Autodesk Software:Inventor Professional 2025 | Vault Professional 2024 | Autocad Mechanical 2024
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:
Drawing List!|
Toggle Drawing Sheet!|
Workplane Resize!|
Drawing View Locker!|
Multi Sheet to Mono Sheet!|
Drawing Weld Symbols!|
Drawing View Label Align!|
Open From Balloon!|
Model State Lock!
Posts and Ideas:
My Ideas|
Dimension Component!|
Partlist Export!|
Derive I-properties!|
Vault Prompts Via API!|
Vault Handbook/Manual!|
Drawing Toggle Sheets!|
Vault Defer Update!

! For administrative reasons, please mark a "Solution as solved" when the issue is solved !


 


EESignature

0 Likes
Message 4 of 6

bradeneuropeArthur
Mentor
Mentor

StrReverse

This one is quite easy to use. As its name suggest StrReverse reverses the letters in a string of text. Here's some code to try:

Dim OriginalText As String
Dim ReversedText As String

OriginalText = "some text"

ReversedText = StrReverse(OriginalText)

MsgBox (ReversedText)

When the code is run, the message box will display "txet emos", which is "some text" reversed.

Regards,

Arthur Knoors

Autodesk Affiliations & Links:
blue LinkedIn LogoSquare Youtube Logo Isolated on White Background


Autodesk Software:Inventor Professional 2025 | Vault Professional 2024 | Autocad Mechanical 2024
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:
Drawing List!|
Toggle Drawing Sheet!|
Workplane Resize!|
Drawing View Locker!|
Multi Sheet to Mono Sheet!|
Drawing Weld Symbols!|
Drawing View Label Align!|
Open From Balloon!|
Model State Lock!
Posts and Ideas:
My Ideas|
Dimension Component!|
Partlist Export!|
Derive I-properties!|
Vault Prompts Via API!|
Vault Handbook/Manual!|
Drawing Toggle Sheets!|
Vault Defer Update!

! For administrative reasons, please mark a "Solution as solved" when the issue is solved !


 


EESignature

Message 5 of 6

A.Acheson
Mentor
Mentor

@WCrihfield 

 

I think I came across this when trying to rework an old piece of ilogic code. I couldn’t figure out why I wasn’t getting the correct string position.

 

I eventually gave up and used the snippets like below. I’m Interest to hear more on this conversation 😏

modelDirectoryName = IO.Path.GetDirectoryName(modelFullFileName)

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 6 of 6

WCrihfield
Mentor
Mentor
Accepted solution

@bradeneuropeArthur, @A.Acheson 

     After reading many more posts online about these functions, I believe I fully understand the difference now.

These two functions were designed more for longer strings which have multiple target strings (or characters) within them.

     Basically, the searching part of what they do is as stated, the InStr starts searching from the left/start, and the InStrRev starts searching from the right/end,  and both are designed to find the 'first' (if multiple) target they find (the instance that is closest to the end it started searching from), however the confusing part is that they both are also designed to return the position (Integer) of that first match as it is counted from the left/start of the source string.

So:

- When there is only one target string within the source string, both functions will always return the same result.

- But when there are multiple target strings within the source string, and the first instances of the target strings aren't at equal distances from the beginning and the end of the source string, the two functions will return different results.

- Neither were designed to "count the characters" to the position of the target from the right/end of the source string.

- The InStr will find the first instance of the target that is closest to the left/start of the source, and return the character count from the left/start to where it was found.

- The InStrRev will find the first instance of the target that is closes to the right/end of the source, and return the character count from the left/start to where it was found.

 

And thanks to @bradeneuropeArthur's suggestion to use StrReverse to reverse the source string, we can use a combination of the regular InStr function and the StrReverse function to retrieve the elusive count from the end of the source string to the first target match instance, closest to the end of the source string like this:

InStr(StrReverse(oSource),oTarget)

where:

'I want it to find the last "C", as counted from the end
'So my returned result should be "1"
Dim oSource As String = "ABCABC"
Dim oTarget As String = "C"
Dim oResult As Integer = InStr(StrReverse(oSource), oTarget)
MsgBox(oResult)

Wesley Crihfield

EESignature

(Not an Autodesk Employee)