Instant methods: find()

The instant method find() searches in the string operand for a character or a substring.

Declaration

int32 string.find( char aChar, int32 aStartIndex )

int32 string.find( string aString, int32 aStartIndex )

Parameters

aChar

A character to search for in the string.

aString

A substring to search for in the string.

aStartIndex

The index of the character within the string to start the search operation. The first character has the index 0, the second character 1, and so far.

Discussion

This first version of the find() method searches in the string operand, in context of which it has been called, for the first occurrence of a character passed in the parameter aChar. The string search operation begins with the character at the position aStartIndex. Within a string the first character has the index 0, the second character 1, and so far. If successful, the method returns the index of the found character or -1 if aChar has not been found in the string. For example:

var string s = "Hello World"; var int32 inx = s.find( 'o', 0 ); // inx = 4 var int32 inx = s.find( 'o', 5 ); // inx = 7 var int32 inx = s.find( 'o', 8 ); // inx = -1

This second version of the find() method searches in the string operand, in context of which it has been called, for the first occurrence of a substring passed in the parameter aString. The string search operation begins with the character at the position aStartIndex. Within a string the first character has the index 0, the second character 1, and so far. If successful, the method returns the index of the found substring or -1 if aString has not been found in the string. For example:

var string s = "Hello World"; var int32 inx = s.find( "World", 0 ); // inx = 6 var int32 inx = s.find( 'World', 7 ); // inx = -1

Please note, the search operations are performed case sensitive: they distinguish between lower and upper case characters. You can use the instant properties lower or upper to convert the operands to lower or upper case before performing the search operation.