Function: StrIndex
Function: StrIndex

Function: StrIndex

StrIndex ( STRING, SUBSTRING )

Gets the character index of the first occurrence of a string in another string.

Parameters

Parameter Type Description
STRING VALUE(STRING) The string to search in.
SUBSTRING VALUE(STRING) The substring to search for.

Returns

VALUE(INTEGER) The index of the first occurrence of the string (0 or greater), or -1 if not found.

What It Does

This function returns the index of the first occurrence of the second string in the first string. If the second string cannot be found in the first, -1 is returned. Both provided values are interpreted as strings before the search. The search is case-sensitive.


	a = strIndex("applesauce", "sauce");     // a = 5
	a = strIndex("banana", "na");            // a = 2
	a = strIndex("tomato", "to");            // a = 0
	a = strIndex("oranges", "stuff");        // a = -1
	a = strIndex(12345, 23);                 // a = 1
	a = strIndex(225.75, "5");               // a = 2
	a = strIndex("ABCDEFG", "a");            // a = -1

Example

String Searching Examples


world
{
	start()
	{
		// Index Search
		textln(strIndex("Apples and Oranges", "Apples"));		// 0
		textln(strIndex("Apples and Oranges", "Pears"));		// -1
		textln("");

		// Index vs. LastIndex
		textln(strIndex("Apples and Oranges", "an"));			// 7 
		textln(strLastIndex("Apples and Oranges", "an"));		// 13
		textln(strIndex("Apples and Oranges", "es"));			// 4 
		textln(strLastIndex("Apples and Oranges", "es"));		// 16
		textln("");

		// Contains
		textln(strContains("Apples and Oranges", "Apples"));	// true 
		textln(strContains("Apples and Oranges", "apples"));	// false 
		textln(strContains("Apples and Oranges", "Pears"));		// false
		textln(strContains(1234, 23));							// true (numbers are converted to strings)
		textln(strContains(0.56, "."));							// true (numbers are converted to strings)
		textln("");

		// Starts With
		textln(strStartsWith("Apples and Oranges", "Apples"));	// true
		textln(strStartsWith("Apples and Oranges", "apples"));	// false (case sensitive)
		textln(strStartsWith("Apples and Oranges", "C"));		// false
		textln(strStartsWith(123456, 123));						// true (numbers are converted to strings)
		textln(strStartsWith(5 + 5, "1"));						// true (numbers are converted to strings)
		textln("");

		// Ends With
		textln(strEndsWith("Apples and Oranges", "Oranges"));	// true
		textln(strEndsWith("Apples and Oranges", "oranges"));	// false (case sensitive)
		textln(strEndsWith("Apples and Oranges", "z"));			// false
		textln(strEndsWith(123456, 456));						// true (numbers are converted to strings)
		textln(strEndsWith(-1.4, ".4"));						// true (numbers are converted to strings)

		quit;
	}
}
×

Modal Header