find
Jump to navigation
Jump to search
Description
- Description:
- Searches for an array element within array or a ANSI string within a ANSI string.
- 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
- Example 3:
- Example 4:
- 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 Jan 04, 2015 - 09:38 (UTC)
- Using nil on either side of find will make the whole statement return Nothing:
- Posted on Apr 10, 2015 - 17:01 (UTC)
- find doesn't work with multidimensional arrays in OFP/CWA. It will always returns -1.
- Posted on May 17, 2016 - 14:21 (UTC)
-
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 Jul 07, 2016 - 10:56 (UTC)
- 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 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]
Categories:
- Scripting Commands
- Introduced with Operation Flashpoint: Elite version 1.00
- Operation Flashpoint: Elite: New Scripting Commands
- Operation Flashpoint: Elite: Scripting Commands
- Operation Flashpoint: Scripting Commands
- ArmA: Armed Assault: Scripting Commands
- Arma 2: Scripting Commands
- Arma 2: Operation Arrowhead: Scripting Commands
- Take On Helicopters: Scripting Commands
- Arma 3: Scripting Commands
- Command Group: Strings
- Command Group: Arrays