StringSearch
 
 
 

StringSearch

Introduced

v5.0

Description

Exposes straightforward string search functionality, based on the InStr function that is built into VBScript.

JScript, Python and other languages provide regular expression support for very powerful string searching capabilities. However this command is provided as a straightfoward solution for the common task of trying to discover whether one string occurs within another.

Unlike VBScript's InStr, this command considers the first character of a string to be index 0. It also has a more conventional ordering of arguments.

Scripting Syntax

oLong = StringSearch( SearchTarget, SearchStr, [CaseSensitive], [StartPos] );

Return Value

(Long) Position in SearchTarget of the first occurance of SearchStr, or -1 if SearchStr was not found. Position is relative to the beginning of the string, even if the StartPos argument was specified.

Parameters

Parameter Type Description
SearchTarget String String to search, can contain newline and other characters
SearchStr String String that we are looking for inside SearchTarget
CaseSensitive Boolean Whether to perform a case-sensitive search or not

Default Value: true

StartPos Long Position to start the search. 0 is the first character

Default Value: 0

Examples

JScript Example

/*
        Example of StringSearch
*/
// Returns 1
Application.LogMessage( StringSearch( "foo", "o" ) ) ;
// Returns -1
Application.LogMessage( StringSearch( "bat", "o" ) ) ;
// Returns 12
Application.LogMessage( StringSearch( "a\nmultiline\nstring", "string" ) ) ;
// Returns -1 (case sensitive)
Application.LogMessage( StringSearch( "foo", "O", true ) ) ;
// Returns 0 (case insensitive)
Application.LogMessage( StringSearch( "bar", "B", false ) ) ;
// Returns -1 (search starting at second character)
Application.LogMessage( StringSearch( "bar", "b", false, 1 ) ) ;

See Also

TrimString