index_of


Description:

public int index_of (string needle, int start_index = 0)

Finds the leftmost occurrence of the given string.

Example: Find the leftmost occurrence of the given string:

public static int main (string[] args) {
string haystack = "Tell me on the way Brigadier! Tell me on the way!";
string needle = "Tell me";
int index = -1;

// Output: ``0``
index = haystack.index_of (needle);
print ("%d\n", index);

// Output: ``30``
index = haystack.index_of (needle, 5);
print ("%d\n", index);

// Output: ``-1``
index = haystack.index_of ("NOT-AVAILABLE", 5);
print ("%d\n", index);

// Output: ``0 30 -1``
index = 0;
do {
index = haystack.index_of (needle, index);
print ("%d ", index);
} while (index++ >= 0);
print ("\n");

return 0;
}

valac --pkg glib-2.0 string.index_of.vala

Parameters:

needle

a string

start_index

the starting position for the search in bytes.

Returns:

the byte index of the first occurrence of the string, or -1 if the string does not occur.