find: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
mNo edit summary
m (Text replacement - "// since Arma 3" to "// since {{arma3}}")
 
(114 intermediate revisions by 14 users not shown)
Line 1: Line 1:
{{Command|= Comments
{{RV|type=command
____________________________________________________________________________________________


| arma |= Game name
|game1= ofpe
|version1= 1.00


|1.00|= Game version
|game2= ofp
____________________________________________________________________________________________
|version2= 1.99


| Searches for an array element within array or a string within a string. Returns the 0 based index on success or -1 if not found. Test is cASe-seNsItiVE | = Description
|game3= arma1
____________________________________________________________________________________________
|version3= 1.00


| array '''find''' x |= Syntax
|game4= arma2
|version4= 1.00


|p1= array: [[Array]] - array to search in |= Parameter 1
|game5= arma2oa
|version5= 1.50


|p2= x: [[Anything]] - array element to find |= Parameter 2
|game6= tkoh
|version6= 1.00


| [[Number]] - 0 based position of the first array element that matches x, -1 if not found |= Return value
|game7= arma3
|version7= 0.50


| s2= string '''find''' x           ''(since ["Arma 3","Arma3",127,126674,"Development"])''|= Syntax
|gr1= Strings


|p21= string: [[String]] - string to search in |= Parameter 1
|gr2= Arrays


|p22= x: [[String]] - string to find |= Parameter 2
|descr= Searches for an array element within an array or an ANSI string within an ANSI string.
{{Feature|arma3|If Unicode support is desired, see [[forceUnicode]].}}
{{Feature|important|Search is cASe-seNsItiVE!}}


| r2= [[Number]] - 0 based position of the first sequence of characters that matches x, -1 if not found |= Return value
|s1= array [[find]] element
____________________________________________________________________________________________
 
|x1= <code>["Apples","Oranges","Pears"] [[find]] "Oranges"; //result is 1
[1,[2],[<nowiki>[</nowiki>3]]] [[find]] <nowiki>[</nowiki>[3]]; //result is 2</code> |= Example 1


|x2= <code>[[if]] ([[magazines]] [[player]] [[find]] "Strela" >= 0) [[then]] {[[hint]] "You've got Strela!"};</code> |= Example 1
|p1= array: [[Array]] - array to search in


|x3= <code>[[hint]] [[str]] ("japa is the man!" [[find]] "the man!"); //8</code> |= Example 3
|p2= element: [[Anything]] - array element to find
____________________________________________________________________________________________


| [[in Array]], [[set]], [[resize]], [[reverse]], [[select]], [[in]], [[toArray]], [[toString]], [[forEach]], [[count]], [[pushBack]], [[deleteAt]], [[deleteRange]]  |= See also
|r1= [[Number]] - zero-based position of the first array element that matches x, -1 if not found


|s2= string [[find]] substring
|s2since= arma3 1.28
|p21= string: [[String]] - string to search in
|p22= substring: [[String]] - substring to find
|r2= [[Number]] - zero-based position of the first sequence of characters that matches substring, -1 if not found
|s3= string [[find]] [substring, indexStart]
|s3since= arma3 2.02
|p41= string: [[String]] - string to search in
|p42= substring: [[String]] - substring to find
|p43= indexStart: [[Number]] - zero-based index which defines where find starts from
|r3= [[Number]] - zero-based position of the first sequence of characters that matches substring, -1 if not found
|x1= <sqf>["Apples", "Oranges", "Pears"] find "Oranges"; // returns 1</sqf>
|x2= <sqf>[1, [2], [[3]]] find [[3]]; // returns 2 - does not work in OFP, returns -1</sqf>
|x3= <sqf>if (magazines player find "Strela" != -1) then { hint "You've got Strela!"; };</sqf>
|x4= <sqf>hint str ("japa is the man!" find "the man!"); // returns 8</sqf>
|x5= <sqf>"abc" find ""; // returns 0</sqf>
|x6= <sqf>
"abcdefghijklmnopqrstuvxyz" find "z"; // returns 24
"abcdefghijklmnopqrstuvxyz" find ["z", 20]; // returns 24, better performance
</sqf>
|x7= <sqf>
"abcßdef" find "c" // 2
"abcßdef" find "ß" // 3
"abcßdef" find "d" // 5 - before forceUnicode
forceUnicode 0; // since {{arma3}} v2.02
"abcßdef" find "d" // 4
</sqf>
|seealso= [[in]] [[findIf]] [[forceUnicode]]
}}
}}


<h3 style="display:none">Notes</h3>
{{Note
<dl class="command_description">
|user= Heeeere's Johnny!
<!-- Note Section BEGIN -->
|timestamp= 20150104093800
|text= Using '''[[nil]]''' on either side of '''find''' will make the whole statement return [[Nothing]]:
<sqf>
_array = [1, 2, nil, 4, 5];
_result = _array find nil;
hintSilent str (isNil "_result"); // true
_result = nil find 1;
hintSilent str (isNil "_result"); // true
</sqf>
}}


<!-- Note Section END -->
{{Note
</dl>
|user= R3vo
 
|timestamp= 20200716073300
<h3 style="display:none">Bottom Section</h3>
|text= 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.
[[Category:Scripting Commands|FIND]]
<sqf>
[[Category:Scripting Commands OFP 1.99|FIND]]
private _fnc_findStringsInString =
[[Category:Scripting Commands ArmA|FIND]]
{
[[Category:Command_Group:_Variables|{{uc:{{PAGENAME}}}}]]
params ["_string", "_search"];
[[Category:Scripting Commands ArmA2|{{uc:{{PAGENAME}}}}]]
if (_string == "") exitWith { [] };
[[Category:Scripting Commands Arma 3|{{uc:{{PAGENAME}}}}]]
private _searchLength = count _search;
[[Category:Scripting_Commands_Take_On_Helicopters|{{uc:{{PAGENAME}}}}]]
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]
</sqf>
}}

Latest revision as of 18:02, 18 November 2023

Hover & click on the images for description

Description

Description:
Searches for an array element within an array or an ANSI string within an 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
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]
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, returns -1
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"; // returns 24 "abcdefghijklmnopqrstuvxyz" find ["z", 20]; // returns 24, better performance
Example 7:
"abcßdef" find "c" // 2 "abcßdef" find "ß" // 3 "abcßdef" find "d" // 5 - before forceUnicode forceUnicode 0; // since Arma 3 v2.02 "abcßdef" find "d" // 4

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
Heeeere's Johnny! - c
Posted on Jan 04, 2015 - 09:38 (UTC)
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
R3vo - c
Posted on Jul 16, 2020 - 07:33 (UTC)
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]