find: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
m (Text replacement - "\| *((\[\[[a-zA-Z0-9_ |()]+\]\],? ?)+) * \}\}" to "|seealso= $1 }}")
(page overhaul, added missing syntax)
Line 1: Line 1:
{{RV|type=command
{{RV|type=command


| arma1
|game1= ofp


|1.00
|version1= 1.99


|game2= ofp
|game2= ofpe


|version2= 1.99
|version2= 1.00
 
|game3= arma1
 
|version3= 1.00
 
|game4= arma2
 
|version4= 1.00
 
|game5= arma2oa
 
|version5= 1.51
 
|game6= tkoh
 
|version6= 1.00
 
|game7= arma3
 
|version7= 0.50


|gr1= Strings
|gr1= Strings
Line 13: Line 33:
|gr2= Arrays
|gr2= Arrays


| Searches for an array element within array or a ANSI string within a ANSI string. Returns the 0 based index on success or -1 if not found.
|descr= Searches for an array element within array or a ANSI string within a ANSI string.
{{Feature | Informative | If Unicode support is desired, see [[forceUnicode]].}}
{{Feature | Arma3| If Unicode support is desired, see [[forceUnicode]].}}
{{Feature | important | Search is cASe-seNsItiVE!}}
{{Feature | important | Search is cASe-seNsItiVE!}}


| array [[find]] element
|s1= array [[find]] element
 
|p1= array: [[Array]] - Array to search in
 
|p2= element: [[Anything]] - Array element to find
 
|r1= [[Number]] - Zero based position of the first array element that matches x, -1 if not found
 
|s2= string [[find]] substring {{GVI|arma3|1.28}}
 
|p21= string: [[String]] - String to search in


|p1= array: [[Array]] - array to search in
|p22= substring: [[String]] - Substring to find


|p2= element: [[Anything]] - array element to find
|r2= [[Number]] - Zero based position of the first sequence of characters that matches substring, -1 if not found


| [[Number]] - 0 based position of the first array element that matches x, -1 if not found
|s3=  string [[find]] [substring, indexStart] {{GVI|arma3|2.02}}


|s2= string [[find]] substring {{Since|arma3|1.27.126674|y}}
|p41= string: [[String]] - String to search in


|p21= string: [[String]] - string to search in
|p42= substring: [[String]] - Substring to find


|p22= substring: [[String]] - substring to find
|p43= indexStart: [[Number]] - Zero based index which defines where find starts from


|r2= [[Number]] - 0 based position of the first sequence of characters that matches substring, -1 if not found
|r3= [[Number]] - Zero based position of the first sequence of characters that matches substring, -1 if not found
   
   
|x1= <code>["Apples", "Oranges", "Pears"] [[find]] "Oranges"; {{cc|returns 1}}</code>
|x1= <code>["Apples", "Oranges", "Pears"] [[find]] "Oranges"; {{cc|returns 1}}</code>
Line 43: Line 73:
|x5= <code>"abc" [[find]] ""; {{cc|returns 0}}</code>
|x5= <code>"abc" [[find]] ""; {{cc|returns 0}}</code>


|seealso= [[set]], [[resize]], [[reverse]], [[select]], [[in]], [[toArray]], [[toString]], [[forEach]], [[count]], [[pushBack]], [[pushBackUnique]], [[apply]], [[deleteAt]], [[deleteRange]], [[append]], [[sort]], [[param]], [[params]], [[arrayIntersect]], [[splitString]], [[joinString]], [[findIf]], [[forceUnicode]]
|x6= <code>"abcdefghijklmnopqrstuvxyz" [[find]] ["z", 20];</code>
 
|seealso= [[in]] [[findIf]] [[forceUnicode]]
}}
}}
[[Category:Scripting Commands OFP 1.99|{{uc:{{PAGENAME}}}}]]
{{GameCategory|arma1|Scripting Commands}}
{{GameCategory|arma2|Scripting Commands}}
{{GameCategory|arma3|Scripting Commands}}
{{GameCategory|tkoh|Scripting Commands}}


<!-- CONTINUE Notes -->
<!-- CONTINUE Notes -->

Revision as of 17:46, 17 March 2021

Hover & click on the images for description

Description

Description:
Searches for an array element within array or a ANSI string within a ANSI string.
Arma 3
If Unicode support is desired, see forceUnicode.
Search is cASe-seNsItiVE!
Groups:
StringsArrays

Syntax 1

Syntax:
array find element
Parameters:
array: Array - Array to search in
element: Anything - Array element to find
Return Value:
Number - Zero based position of the first array element that matches x, -1 if not found

Syntax 2

Syntax:
string find substring Arma 3 logo black.png1.28
Parameters:
string: String - String to search in
substring: String - Substring to find
Return Value:
Number - Zero based position of the first sequence of characters that matches substring, -1 if not found

Syntax 3

Syntax:
string find [substring, indexStart] Arma 3 logo black.png2.02
Parameters:
string: String - String to search in
substring: String - Substring to find
indexStart: Number - Zero based index which defines where find starts from
Return Value:
Number - Zero based position of the first sequence of characters that matches substring, -1 if not found

Examples

Example 1:
["Apples", "Oranges", "Pears"] find "Oranges"; // returns 1
Example 2:
[1, [2], [[3]]] find [[3]]; // returns 2 - does not work in OFP
Example 3:
if (magazines player find "Strela" != -1) then { hint "You've got Strela!"; };
Example 4:
hint str ("japa is the man!" find "the man!"); // returns 8
Example 5:
"abc" find ""; // returns 0
Example 6:
"abcdefghijklmnopqrstuvxyz" find ["z", 20];

Additional Information

See also:
in findIf forceUnicode

Notes

Report bugs on the Feedback Tracker and/or discuss them on the Arma Discord or on the Forums.
Only post proven facts here! Add Note
Posted on January 4, 2015 - 09:38 (UTC)
Heeeere's Johnny!
Using nil on either side of find will make the whole statement return Nothing: _array = [1,2,nil,4,5]; _result = _array find nil; hintSilent str (isNil "_result"); //true _result = nil find 1; hintSilent str (isNil "_result"); //true
Posted on April 10, 2015 - 17:01 (UTC)
Kenoxite
find doesn't work with multidimensional arrays in OFP/CWA. It will always returns -1.
Posted on May 17, 2016 - 14:21 (UTC)
BaerMitUmlaut
This command is unreliable/broken when it comes to some non-ASCII characters (as of Arma 3 1.58): "abcßdef" find "c" -> 2 "abcßdef" find "ß" -> 3 "abcßdef" find "d" -> 5
Posted on July 7, 2016 10:56 (UTC)
Jtgibson
Not quite unreliable, just unexpected! Strings are tracked in terms of bytes rather than in actual character positions; all strings are stored in UTF-8 format. In other words, the eszett character is in Unicode, which takes up two bytes rather than one as it is within the 128-255 range of Unicode. (Similar results would be expected for the division symbol, the umlaut, accented e's, etc.) Symbols that are particularly high in the Unicode range may take up three bytes, or even four for the truly exceptional characters, although Arma 3's default fonts are unlikely to render them. This definitely complicates any script which assumes any printable character is a single byte, however, and unfortunately I'm not skilled enough with internationalisation to recommend any robust fix.
Posted on July 16, 2020 - 07:33 (UTC)
R3vo
If you want to return all occurences of a given string in a string use the following code. Thanks to sharp. for providing the code.
private _fnc_findStringsInString = { params ["_string", "_search"]; if (_string == "") exitWith {[]}; private _searchLength = count _search; private _return = []; private _i = 0; private _index = 0; while {_index = _string find _search; _index != -1} do { _string = _string select [_index + _searchLength]; _i = _i + _index + _searchLength; _return pushBack _i - _searchLength; }; _return }; ["Test,123,123,Test","Test"] call _fnc_findStringsInString;//Returns [0,13]