find: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
m (Text replacement - "\[\[Category:[ _]?Scripting[ _]Commands[ _]Take[ _]On[ _]Helicopters(\|.*)?\]\]" to "{{GameCategory|tkoh|Scripting Commands}}")
m (Text replacement - "// since Arma 3" to "// since {{arma3}}")
 
(69 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{Command|Comments=
{{RV|type=command
____________________________________________________________________________________________


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


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


|game2= ofp |Game2 name=
|game3= arma1
|version3= 1.00


|version2= 1.99 |Game2 version=
|game4= arma2
|version4= 1.00


|gr1= Strings |GROUP1=
|game5= arma2oa
|version5= 1.50


|gr2= Arrays |GROUP2=
|game6= tkoh
____________________________________________________________________________________________
|version6= 1.00


| 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.
|game7= arma3
{{Informative | If Unicode support is desired, see [[forceUnicode]].}}
|version7= 0.50
{{Important | Search is cASe-seNsItiVE!}} |DESCRIPTION=
____________________________________________________________________________________________


| array [[find]] element |SYNTAX=
|gr1= Strings


|p1= array: [[Array]] - array to search in |PARAMETER1=
|gr2= Arrays


|p2= element: [[Anything]] - array element to find |PARAMETER2=
|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!}}


| [[Number]] - 0 based position of the first array element that matches x, -1 if not found |RETURNVALUE=
|s1= array [[find]] element


|s2= string [[find]] substring {{since|arma3|1.27.126674|y}} |SYNTAX2=
|p1= array: [[Array]] - array to search in


|p21= string: [[String]] - string to search in |PARAMETER21=
|p2= element: [[Anything]] - array element to find


|p22= substring: [[String]] - substring to find |PARAMETER22=
|r1= [[Number]] - zero-based position of the first array element that matches x, -1 if not found


|r2= [[Number]] - 0 based position of the first sequence of characters that matches substring, -1 if not found |RETURNVALUE2=
|s2= string [[find]] substring
____________________________________________________________________________________________
 
|x1= <code>["Apples", "Oranges", "Pears"] [[find]] "Oranges"; {{cc|returns 1}}</code> |EXAMPLE1=


|x2= <code>[1, [2], [<nowiki/>[3]]] [[find]] [<nowiki/>[3]]; {{cc|returns 2 - does not work in OFP}}</code> |EXAMPLE2=
|s2since= arma3 1.28


|x3= <code>[[if]] ([[magazines]] [[player]] [[find]] "Strela" != -1) [[then]] { [[hint]] "You've got Strela!"; };</code> |EXAMPLE3=
|p21= string: [[String]] - string to search in


|x4= <code>[[hint]] [[str]] ("japa is the man!" [[find]] "the man!"); {{cc|returns 8}}</code> |EXAMPLE4=
|p22= substring: [[String]] - substring to find


|x5= <code>"abc" [[find]] ""; {{cc|returns 0}}</code> |EXAMPLE5=
|r2= [[Number]] - zero-based position of the first sequence of characters that matches substring, -1 if not found
____________________________________________________________________________________________


| [[set]], [[resize]], [[reverse]], [[select]], [[in]], [[toArray]], [[toString]], [[forEach]], [[count]], [[pushBack]], [[pushBackUnique]], [[apply]], [[deleteAt]], [[deleteRange]], [[append]], [[sort]], [[param]], [[params]], [[arrayIntersect]], [[splitString]], [[joinString]], [[findIf]], [[forceUnicode]] |SEEALSO=
|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>


<h3 style="display:none">Notes</h3>
|x3= <sqf>if (magazines player find "Strela" != -1) then { hint "You've got Strela!"; };</sqf>
<dl class="command_description">
<!-- Note Section BEGIN -->


<!-- Note Section END -->
|x4= <sqf>hint str ("japa is the man!" find "the man!"); // returns 8</sqf>
</dl>


<h3 style="display:none">Bottom Section</h3>
|x5= <sqf>"abc" find ""; // returns 0</sqf>
[[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 -->
|x6= <sqf>
<dl class="command_description">
"abcdefghijklmnopqrstuvxyz" find "z"; // returns 24
<dd class="notedate">Posted on January 4, 2015 - 09:38 (UTC)</dd>
"abcdefghijklmnopqrstuvxyz" find ["z", 20]; // returns 24, better performance
<dt class="note">[[User:Heeeere's Johnny!|Heeeere's Johnny!]]</dt>
</sqf>
<dd class="note">
Using '''[[nil]]''' on either side of '''find''' will make the whole statement return [[Nothing]]:
<code>_array = [1,2,[[nil]],4,5];
_result = _array '''find''' nil;
[[hintSilent]] [[str]] ([[isNil]] "_result"); //true
_result = nil '''find''' 1;
[[hintSilent]] [[str]] ([[isNil]] "_result"); //true</code>
</dd>


<dd class="notedate">Posted on April 10, 2015 - 17:01 (UTC)</dd>
|x7= <sqf>
<dt class="note">[[User:Kenoxite|Kenoxite]]</dt>
"abcßdef" find "c" // 2
<dd class="note">
"abcßdef" find "ß" // 3
[[find]] doesn't work with multidimensional arrays in OFP/CWA. It will always returns -1.
"abcßdef" find "d" // 5 - before forceUnicode
</dd>


<dd class="notedate">Posted on May 17, 2016 - 14:21 (UTC)</dd>
forceUnicode 0; // since {{arma3}} v2.02
<dt class="note">[[User:BaerMitUmlaut|BaerMitUmlaut]]</dt>
"abcßdef" find "d" // 4
<dd class="note">
</sqf>
This command is unreliable/broken when it comes to some non-ASCII characters (as of Arma 3 1.58):
<code>"abcßdef" find "c"
-> 2
"abcßdef" find "ß"
-> 3
"abcßdef" find "d"
-> 5</code>
</dd>


<dd class="notedate">Posted on July 7, 2016 10:56 (UTC)</dd>
|seealso= [[in]] [[findIf]] [[forceUnicode]]
<dt class="note">[[User:Jtgibson|Jtgibson]]</dt>
}}
<dd class="note">
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.
</dd>


<dd class="notedate">Posted on July 16, 2020 - 07:33 (UTC)</dd>
{{Note
<dt class="note">[[User:R3vo|R3vo]]</dt>
|user= Heeeere's Johnny!
<dd class="note">
|timestamp= 20150104093800
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.<br>
|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>
}}


<code>[[private]] _fnc_findStringsInString [[=]]
{{Note
{
|user= R3vo
    [[params]] ["_string", "_search"];  
|timestamp= 20200716073300
    [[if]] (_string [[==]] "") [[exitWith]] {[]};  
|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.
    [[private]] _searchLength = [[count]] _search;  
<sqf>
    [[private]] _return [[=]] [];  
private _fnc_findStringsInString =
    [[private]] _i [[=]] 0;  
{
    [[private]] _index [[=]] 0;  
params ["_string", "_search"];
    [[while]] {_index [[=]] _string [[find]] _search; _index [[!=]] -1} [[do]]
if (_string == "") exitWith { [] };
    {  
private _searchLength = count _search;
        _string [[=]] _string [[select]] [_index [[+]] _searchLength];  
private _return = [];
        _i [[=]] _i [[+]] _index [[+]] _searchLength;  
private _i = 0;
        _return [[pushBack]] _i [[-]] _searchLength;  
private _index = 0;
    };  
while { _index = _string find _search; _index != -1 } do
    _return  
{
};  
_string = _string select [_index + _searchLength];
["Test,123,123,Test","Test"] [[call]] _fnc_findStringsInString;//Returns [0,13]</code>
_i = _i + _index + _searchLength;
</dd>
_return pushBack _i - _searchLength;
</dl>
};
<!-- DISCONTINUE Notes -->
_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]