Changing comparison test of FolderItem Path from InStr to IndexOf fails

I changed the comparison test of a FolderItem path from InStr to IndexOf and it now fails.

I have this function called

InPfCo(srce As FolderItem) Return Integer

srce and DocFolder as NativePath

[quote]C:\Users\algab\Documents\Korean Language\TextFiles\Special\KimChi Chronicles.txt
C:\Users\algab\Documents\Korean Language\TextFiles\[/quote]

The only text which changes is the Api2 IndexOf for Instr
Using this construction the comparison succeeds

Var its As String If 0 < srce.NativePath.InStr(DocFolder.NativePath) Then its = App.FitemToPPath( srce, DocFolder ) Else Return -2 End If

Using this construction it fails

Var its As String If 0 < srce.NativePath.IndexOf(DocFolder.NativePath) Then its = App.FitemToPPath( srce, DocFolder ) Else Return -2 End If

Am I missing something?

seems to work ok here in quick test

Var srceNativePath As String = "this is a test\\for something"
Var DocFolderNativePath As String = "is a test\"


If 0 < srceNativePath.InStr(DocFolderNativePath) Then
  Break // its = App.FitemToPPath( srce, DocFolder )
Else
  Break // Return -2
End If


If 0 < srceNativePath.IndexOf(DocFolderNativePath) Then
  Break //its = App.FitemToPPath( srce, DocFolder )
Else
  Break //Return -2
End If

maybe pull some of those paths into local variables and inspect them ?
make sure they all have encodings etc

IndexOf is zero-based. Your code will fail if the match is at the beginning of the string. IndexOf is not a drop-in replacement for InStr.

So what do I use with indexof to make it work?

If I put a zero or a 1 for the starting point it fails

Use -1 for your comparison.

Sorry. -1 doesn’t work either

Also. String.Compare does work

Odd, this should be working.

If -1 < srce.NativePath.IndexOf(DocFolder.NativePath) Then
  its = App.FitemToPPath( srce, DocFolder )
Else
  Return -2
End If

Oh. I thought -1 was the index as in inside the parentheses

Yes. It works.

Now I can set the test to be

If  0 = srce.NativePath.IndexOf(DocFolder.NativePath) Then

Sorry I missed that. That “0” is a lot more satisfying for a result with 0-based arrays.

If you’re only interested in a match at the beginning of the string, wouldn’t BeginsWith be clearer?

if src.NativePath.BeginsWith(DocFolder.NativePath) Then

I didn’t even notice BeginsWith was there. Much easier.
Thanks.

I think I got lost in the basic coding examples.

Just found the new method “IsEmpty”