7erra 629 Posted April 11, 2017 (edited) --------------------------------- UPDATE 29.01.2022 With Arma 3 2.06+ it is now possible to use regexReplace instead: https://community.bistudio.com/wiki/regexReplace Performance: ["12345 123456", "123", "abc"] call TER_fnc_editString; Execution Time: 0.0093 ms | Cycles: 10000/10000 | Total Time: 93 ms "12345 123456" regexReplace ["123/g", "abc"] Execution Time: 0.0016 ms | Cycles: 10000/10000 | Total Time: 16 ms --------------------------------- Hello everyone, I just wanted to share a small function. It can replace certain parts of a string with your own string. /* Author: Terra Description: Substitute a certain part of a string with another string. Parameters: 1: STRING - Source string 2: STRING - Part to edit 3: STRING - Substitution Returns: STRING Example: ["12345 123456", "123", "abc"] call TER_fnc_editString; Returns: "abc45 abc456" */ TER_fnc_editString = { params ["_str", "_toFind", "_subsitution"]; _char = count _toFind; _no = _str find _toFind; while {-1 != _str find _toFind} do { _no = _str find _toFind; _splitStr = _str splitString ""; _splitStr deleteRange [(_no +1), _char -1]; _splitStr set [_no, _subsitution]; _str = _splitStr joinString ""; }; _str }; Additional information: Spoiler Other topics: http://kronzky.info/snippets/strings/index.htm - A collection of string manipulating functions. Sadly it seems not avaliable anymore. ^ - Other options for similar results _________________________________________________________________________________________________________ You can also add the function to the function library: // description.ext: // Functions class CfgFunctions { class TER { class mainFnc { class editString {file = "filePaht\editString.sqf";}; }; }; }; ////////////////////////////////////////////////////////////////////////////////////// // editString.sqf: /* Author: Terra Description: Substitute a certain part of a string with another string. Parameters: 1: STRING - Source string 2: STRING - Part to edit 3: STRING - Substitution Returns: STRING Example: ["12345 123456", "123", "abc"] call TER_fnc_editString; Returns: "abc45 abc456" */ params ["_str", "_toFind", "_subsitution"]; _char = count _toFind; _no = _str find _toFind; while {-1 != _str find _toFind} do { _no = _str find _toFind; _splitStr = _str splitString ""; _splitStr deleteRange [(_no +1), _char -1]; _splitStr set [_no, _subsitution]; _str = _splitStr joinString ""; }; _str Have fun editing, Terra Edited January 29, 2022 by 7erra regexReplace 1 Share this post Link to post Share on other sites
7erra 629 Posted April 16, 2017 New version: v1.1 ADDED: Edit more than one part of your source string FIXED: "Safety switch" so the function doesn't run infinitely and crash the game /* Author: Terra Description: Substitute a certain part of a string with another string. Parameters: 1: STRING - Source string 2: STRING - Part to edit * ARRAY OF STRINGS - Multiple parts to edit 3: STRING - Substitution 4: (Optional) NUMBER - maximum substitutions * Default: 10 5: (Optional) CASE - Enable maximum limit of substitutions (WARNING: Substituting an expression with the same expression will lead to infinite loops) * Default: true Returns: STRING */ _TER_fnc_editString = { params ["_str", "_toFind", "_subsitution", ["_numLimit",10,[1]], ["_limit",true,[true]]]; if (typeName _toFind != typeName []) then {_toFind = [_toFind]}; { _char = count _x; _no = _str find _x; private _loop = 0; while {-1 != _str find _x && _loop < _numLimit} do { _no = _str find _x; _splitStr = _str splitString ""; _splitStr deleteRange [(_no +1), _char -1]; _splitStr set [_no, _subsitution]; _str = _splitStr joinString ""; if (_limit) then {_loop = _loop +1;}; }; } forEach (_toFind); _str }; Examples: Spoiler Example 1: ["12345 123456", "123", "abc"] call TER_fnc_editString; Returns: "abc45 abc456" Example 2: ["123456 123456 TEST", ["12", "56", "TEST"], "abc"] call TER_fnc_editString; Returns: "abc34abc abc34abc abc" Example 3: ["123 123", "123", " 123"] call TER_fnc_editString; Returns: " 123 123" // 10 substitutions ["123 123", "123", " 123", 20] call TER_fnc_editString; Returns: " 123 123" // 20 substitutions ["123 123", "123", " 123", 10, false] call TER_fnc_editString; ! Infinite loop and crash ! Have fun editing, Terra 1 Share this post Link to post Share on other sites