Function: StrLastIndex
Function: StrLastIndex

Function: StrLastIndex

StrLastIndex ( STRING, SUBSTRING )

Gets the character index of the last 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 last occurrence of the string (0 or greater), or -1 if not found.

What It Does

This function returns the index of the last occurrence of the second string in the first string (search starts from end of 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 = strLastIndex("applesauce", "sauce");     // a = 5
	a = strLastIndex("banana", "na");            // a = 4
	a = strLastIndex("tomato", "to");            // a = 4
	a = strLastIndex("oranges", "stuff");        // a = -1
	a = strLastIndex(12345, 23);                 // a = 1
	a = strLastIndex(225.75, "5");               // a = 5
	a = strLastIndex("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