Utf8.find: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(Created page with "__NOTOC__ {{Shared function}} Finds the first occurrence of the [http://lua-users.org/wiki/PatternsTutorial pattern] in the string passed. If an instance of the pattern is fo...")
 
No edit summary
Line 9: Line 9:
===Required Arguments===
===Required Arguments===
*'''input:''' A string character sequence
*'''input:''' A string character sequence
*'''pattern :''' A string match [http://lua-users.org/wiki/PatternsTutorial pattern] (you can disable pattern matching by using the optional fourth argument ''plain'')
*'''pattern:''' A string match [http://lua-users.org/wiki/PatternsTutorial pattern] (you can disable pattern matching by using the optional fourth argument ''plain'')


===Optional Arguments===
===Optional Arguments===

Revision as of 03:01, 15 February 2016

Finds the first occurrence of the pattern in the string passed. If an instance of the pattern is found, a pair of values representing the start and the end of the matched string is returned.

Syntax

string utf8.find ( string input, string pattern [, int startpos = 1, boolean plain = false ] )

Required Arguments

  • input: A string character sequence
  • pattern: A string match pattern (you can disable pattern matching by using the optional fourth argument plain)

Optional Arguments

NOTE: When using optional arguments, you might need to supply all arguments before the one you wish to use. For more information on optional arguments, see optional arguments.

  • startpos: A number representing the beginning position.
  • plain: A boolean, if pattern matching should be turned off

Returns

Returns two number values for the beginning and ending position of the matched string, nil otherwise.

Example

Click to collapse [-]
Server

This example shows how to search for parts of a string.

print( utf8.find( "Hello MTA User", "User" ) ) -- 11, 14
print( utf8.find( "Hello MTA User", "e" ) ) -- 2, 2
print( utf8.find( "Hello MTA User", "e", 3 ) ) -- 13, 13
print( utf8.find( "Привет Привет", "%s" ) ) -- 7, 7
print( utf8.find( "Привет Привет", "%s", 1, true ) ) -- nil

-- Comparsion of utf8.find and string.find
local startpos, endpos = utf8.find( "Привет", "и" )
print( startpos, endpos ) -- 3, 3

local startpos, endpos = string.find( "Привет", "и" )
print( startpos, endpos ) -- 5, 6

See Also