Hard time with IndexOf

The code below do not works as expected. I mean, the code in the If Condition is never run.
NB: I used ComparisonOptions.Casesensitive sith the Same result.

ideas ?

If aLine.IndexOf("Europe,Afrique,Asie,Amérique,Océanie",ComparisonOptions.Caseinsensitive,French_Loc) <> -1 Then

No visual feedback I was avaiting, so I added System.DebugLog "In if block" and run: nothing.

What is the aLine string?

The aLine string comes from a text file and holds a country name (simplified / expanded).
It is added in a ListBox (previous line).

And, yes, I have these Continent names in the text (aLine) at a moment.

This comes from a window that displays the list of countries by Continent in a ListBox; The If Block is here to add a Tag to change the background color for Continent name (only).

I used “EuropeAfriqueAsieAmériqueOcéanie” with the same failure.

Is your usage not back to front? aLine should contain the long string and the argument to indexof the short string??

Var aLine As String = "amÉrique"

Var validList As String = "Europe,Afrique,Asie,Amérique,Océanie,"

Var found As Boolean = validList.IndexOf(aLine.Trim+",") >= 0

If found Then MessageBox("Amérique was found!")

Ha! Obviously not thinking clearly at the minute - comes of being in hospital.

1 Like


Const list As String = "Europe,Afrique,Asie,Amérique,Océanie"

Var aLine As String = "amérique"

Var options() As String = list.Split(",")

Var which As Integer = options.IndexOf(aLine.Trim)

Var found As Boolean = which >= 0

If found Then MessageBox("Found! It's "+options(which))

Quit

Thank you Rick.

We are far away from InStr !

Tim:
I’ve used McDo automat for meal, get my VISA and get the ticket to pay with real money !

Sometimes…

1 Like

I was verbose just to be clear, dissecting parts. And showing a way to determine what option (which) you got. :wink:

I am stoned (may need a nap ?).
I want to know if aLine contains any of the continent names.
That’s why I coded aLine.IndexOf(StringtoSearch…)

And now I feel lost; I don’t know if this is what I want to do, or how to do it.

aLine is a line read from a Text file and added on a ListBox.
The code coul be five times:

If aLine = "Amérique" Then
// Add a Row Tag
End If

But now look at a gas manufacture.

Is it clear ?

Yes, as the examples I’ve shown.

Copy and past the codes in a sample and debug to understand how it works.

Take a nap first. :wink:

Maybe what you are looking for is more like:

Var aLine As String = "amérique"

Select Case aline.Trim
  
case "Amérique"
  MessageBox "Was #1, Amérique"
  
Case "Europe"
  MessageBox "Was #2, Europe"
  
Else
  MessageBox "Not found"
  
End

I just tested:

If aLine = "Afrique" Then
LB_Pays.CellTagAt(LB_Pays.LastAddedRowIndex,0) = "Continent"
End If

And I get what I want. I just need to add… Europe, Oéanie, Amérique…

Here I am:


Select Case aLine
Case "Afrique","Amérique du nord","Amérique du sud","Asie","Europe","Océanie"
  LB_Pays.CellTagAt(LB_Pays.LastAddedRowIndex,0) = "Continent"
  
End Select

One useful feature of the PHP docs is that it is made quite clear as to which is the needle and which is the haystack.

Logic states that in a expression

whatever.IndexOf( thing )

thing must be the needle being searched in the haystack.

IOW,

Haystack.indexof (needle)

2 Likes

Came with documentation:
users cry agianst the documentation
Company change the presentation (but keep the same text).

Another analogy:
A huy said: “There is no sun today”

A clown answer: “No, there is sun today, like everyday, the sun is on the other side of the clouds.”