Difference between revisions of "String.find"

From GiderosMobile
m (Text replacement - "</source>" to "</syntaxhighlight>")
 
(9 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
__NOTOC__
 
__NOTOC__
 
'''Available since:''' Gideros 2011.6<br/>
 
'''Available since:''' Gideros 2011.6<br/>
 +
'''Class:''' [[string]]<br/>
 +
 
=== Description ===
 
=== Description ===
Looks for the first match of pattern in the string s. If it finds a match, then find returns the indices of s where this occurrence starts and ends; otherwise, it returns nil. A third, optional numerical argument init specifies where to start the search; its default value is 1 and can be negative. A value of true as a fourth, optional argument plain turns off the pattern matching facilities, so the function does a plain "find substring" operation, with no characters in pattern being considered "magic". Note that if plain is given, then init must be given as well.  
+
Looks for the first match of ''pattern'' in the string ''s''.
 +
<syntaxhighlight lang="lua">
 +
(numbers) = string.find(s,pattern,init,plain)
 +
</syntaxhighlight>
 +
 
 +
 
 +
If it finds a match, then '''find''' returns the indices of ''s'' where this occurrence starts and ends; otherwise, it returns nil.
 +
 
 +
A third, optional numerical argument ''init'' specifies where to start the search; its default value is 1 and can be negative.
 +
 
 +
A value of true as a fourth, optional argument ''plain'' turns off the pattern matching facilities, so the function does a plain "find substring" operation, with no characters in pattern being considered "magic". Note that if plain is given, then init must be given as well.  
  
 +
If the pattern has captures, then in a successful match the captured values are also returned, after the two indices.
  
If the pattern has captures, then in a successful match the captured values are also returned, after the two indices.
+
=== Parameters ===
<source lang="lua">
+
'''s''': (string) string where to find a pattern<br/>
(numbers) = string.find(s,pattern,init,plain)
+
'''pattern''': (string) pattern to find<br/>
</source>
+
'''init''': (number) index where to start searching for pattern '''optional''' (default = 1)<br/>
'''s''': (string) string where to find a pattern ''''''<br/>
+
'''plain''': (bool) if true, then it simply matches substring without any special symbols '''optional''' (default = false)<br/>
'''pattern''': (string) pattern to find ''''''<br/>
+
 
'''init''': (number) index where to start searching for pattern '''optional'''<br/>
+
=== Return values ===
'''plain''': (bool) if true, then it simply matches substring without any special symbols (default = false) '''optional'''<br/>
+
'''Returns''' (numbers) indices of occurrence of the pattern or nil<br/>
'''Returns''' (numbers) indexes of occurrence of the pattern or nil<br/>
+
 
 +
=== Example ===
 +
<syntaxhighlight lang="lua">
 +
local mystring = "Hello World! This is a beautiful world."
 +
local g, h = mystring:find("World")
 +
if g then print(g, h) end -- outputs 7 11
 +
</syntaxhighlight>
 +
 
 +
{{String}}

Latest revision as of 14:33, 13 July 2023

Available since: Gideros 2011.6
Class: string

Description

Looks for the first match of pattern in the string s.

(numbers) = string.find(s,pattern,init,plain)


If it finds a match, then find returns the indices of s where this occurrence starts and ends; otherwise, it returns nil.

A third, optional numerical argument init specifies where to start the search; its default value is 1 and can be negative.

A value of true as a fourth, optional argument plain turns off the pattern matching facilities, so the function does a plain "find substring" operation, with no characters in pattern being considered "magic". Note that if plain is given, then init must be given as well.

If the pattern has captures, then in a successful match the captured values are also returned, after the two indices.

Parameters

s: (string) string where to find a pattern
pattern: (string) pattern to find
init: (number) index where to start searching for pattern optional (default = 1)
plain: (bool) if true, then it simply matches substring without any special symbols optional (default = false)

Return values

Returns (numbers) indices of occurrence of the pattern or nil

Example

local mystring = "Hello World! This is a beautiful world."
local g, h = mystring:find("World")
if g then print(g, h) end -- outputs 7 11