Duck hunt
/ /

XtScript Function: strpos and stripos

Find the first occurrence of a case-sensitive, or case-insensitive substring and return the position.

If you want to perform a search-alike operation, there are two XtScript built-in functions may can achieve it by finding the position of a substring from a string which are strpos and stripos.

Both strpos and stripos will perform a search of a substring and return the first occurrence of the substring position.

The difference between strpos and stripos is where strpos will perform the operation as CASE-SENSITIVE, while stripos will perform it as CASE-INSENSITIVE.

XtScript strpos and stripos function basic syntax:

<!--parser:xtscript-->
	# Find the first occurrence of a case-sensitive substring and return the position
	call strpos $haystack=...; $needle=...; $offset=...

	# Find the first occurrence of a case-insensitive substring and return the position
	call stripos $haystack=...; $needle=...; $offset=...
<!--/parser:xtscript-->

Available arguments in XtScript strpos and stripos function:

ArgumentValueMandatoryExplanation
$haystackstringRequiredSpecify the main string where the substring will be searched in.
$needlestringRequiredSpecify the substring that searched for.
$offsetnumberOptionalSpecify the index of a character the search will be started from.

Learn XtScript strpos and stripos functions from examples

Search for a substring from the main string and return the first occurrence of the substring position using XtScript strpos function.

- Code:
<!--parser:xtscript-->
	var $animal = Chicken | Duck | Elephant | Llama | Sheep | Whale

	# Search for "Duck" as case-sensitive in $animal 
	var $find_Duck = call strpos $haystack=$animal; $needle=Duck

	# Search for "sheep" as case-sensitive in $animal
	var $find_sheep = call strpos $haystack=$animal; $needle=sheep

	# Perform IF conditional operation on "Duck" search result
	if $find_Duck
		print "Duck" is found in the \$animal . Duck is on position: $find_Duck<br/><br/>
	else
		print "Duck" cannot be found in the \$animal list <br/><br/>
	endif

	# Perform IF conditional operation on "sheep" as case insensitive
	if $find_sheep
		print "sheep" is found in the \$animal . sheep is on position: $find_sheep<br/><br/>
	else
		print "sheep" cannot be found in the \$animal list
	endif
<!--/parser:xtscript-->

<p>The $find_sheep above is return false because we are using <span class="w3-codespan">strpos</span> function, "sheep" is not same with "Sheep" </p>
- Result:
"Duck" is found in the $animal . Duck is on position: 10

"sheep" cannot be found in the $animal list

The $find_sheep above is return false because we are using strpos function, "sheep" is not same with "Sheep"

Remember that index in programming like Javascript, PHP, and incuding XtScript is started at 0.

Search of a substring in the main string as CASE-INSENSITIVE and return the first occurrence of the substring position using XtScript stripos function.

- Code:
<!--parser:xtscript-->
	var $string = Well, let's learn XtScript shall we?
	var $sea = We
	var $offs = 4

	print We is found on position: <b>

	# Search "We" as case-insesitive in the $string started from $offset and return the position
	call stripos $haystack=$string; $needle=$sea; $offset=1

	print </b>
<!--/parser:xtscript-->
- Result:
We is found on position: 33

We is returned as position 33 although We can be found as the first character at position 0 from Well word. This is because we are set the $offset to 1, it means the function will start searching from index 1 which is from e (in Well word).