Jump to content
Sign in to follow this  
Ed!

replaceString?

Recommended Posts

I needed a function to replace a certain set of characters within a string. I found BIS_fnc_splitString and BIS_fnc_trimString etc. but not a replaceString. So I made it myself, but usually there is always somebody afterwards posting a simple 1 liner that does everything I tried in my 100 lines - and Im here asking for it.

This is what I got

strings_replace = 
{
_string = _this select 0;
_replace = _this select 1;
_replaceWith = _this select 2;
_strar = toArray (_string);
_replaceAr = toArray (_replace);
_replaceWithAr = toArray (_replaceWith);
_newStrAr = [];
for[{_i = 0}, {_i < count(_strar)}, {_i = _i + 1}] do
{
	if((_strar select _i) == (_replaceAr select 0)) then
	{
		_match = true;
		for[{_k = 0}, {((_k + _i) < count(_strar)) && (_k < count(_replaceAr))}, {_k = _k + 1}] do
		{
			if((_strar select (_k + _i)) != (_replaceAr select _k)) then 
			{
				_match = false;
			};
		};
		if(_match) then
		{
			for[{_j = 0}, {_j < count(_replaceWithAr)}, {_j = _j + 1}] do
			{
				_newStrAr = _newStrAr + [_replaceWithAr select _j];
			};
			_i = _i + count(_replaceAr) - 1;
		}
		else
		{
			_newStrAr = _newStrAr + [_strar select _i];
		};
	}
	else
	{
		_newStrAr = _newStrAr + [_strar select _i];
	};
};
toString (_newStrAr);
};

Or did somebody out there make a whole library of string manipulation functions for us to use?

Share this post


Link to post
Share on other sites

You can already use substring functionality:

[color="#FF8040"][color="#006400"][i]//[string, replacewhat, replacewithwhat][/i][/color]
KK_fnc_strReplace [color="#8B3E2F"][b]=[/b][/color] [color="#8B3E2F"][b]{[/b][/color]
[color="#191970"][b]private[/b][/color] [color="#8B3E2F"][b][[/b][/color][color="#7A7A7A"]"_s"[/color][color="#8B3E2F"][b],[/b][/color][color="#7A7A7A"]"_f"[/color][color="#8B3E2F"][b],[/b][/color][color="#7A7A7A"]"_r"[/color][color="#8B3E2F"][b],[/b][/color][color="#7A7A7A"]"_i"[/color][color="#8B3E2F"][b]][/b][/color][color="#8B3E2F"][b];[/b][/color]
[color="#1874CD"]_s[/color] [color="#8B3E2F"][b]=[/b][/color] [color="#000000"]_this[/color] [color="#191970"][b]select[/b][/color] [color="#FF0000"]0[/color][color="#8B3E2F"][b];[/b][/color]
[color="#1874CD"]_f[/color] [color="#8B3E2F"][b]=[/b][/color] [color="#000000"]_this[/color] [color="#191970"][b]select[/b][/color] [color="#FF0000"]1[/color][color="#8B3E2F"][b];[/b][/color]
[color="#1874CD"]_r[/color] [color="#8B3E2F"][b]=[/b][/color] [color="#000000"]_this[/color] [color="#191970"][b]select[/b][/color] [color="#FF0000"]2[/color][color="#8B3E2F"][b];[/b][/color]
[color="#1874CD"]_i[/color] [color="#8B3E2F"][b]=[/b][/color] [color="#1874CD"]_s[/color] [color="#191970"][b]find[/b][/color] [color="#1874CD"]_f[/color][color="#8B3E2F"][b];[/b][/color]
[color="#191970"][b]if[/b][/color] [color="#8B3E2F"][b]([/b][/color][color="#1874CD"]_i[/color] [color="#8B3E2F"][b]<[/b][/color] [color="#FF0000"]0[/color][color="#8B3E2F"][b])[/b][/color] [color="#191970"][b]exitWith[/b][/color] [color="#8B3E2F"][b]{[/b][/color][color="#1874CD"]_s[/color][color="#8B3E2F"][b]}[/b][/color][color="#8B3E2F"][b];[/b][/color]
[color="#8B3E2F"][b]([/b][/color][color="#1874CD"]_s[/color] [color="#191970"][b]select[/b][/color] [color="#8B3E2F"][b][[/b][/color][color="#FF0000"]0[/color][color="#8B3E2F"][b],[/b][/color] [color="#1874CD"]_i[/color][color="#8B3E2F"][b]][/b][/color][color="#8B3E2F"][b])[/b][/color] [color="#8B3E2F"][b]+[/b][/color] [color="#1874CD"]_r[/color] [color="#8B3E2F"][b]+[/b][/color] [color="#8B3E2F"][b]([/b][/color][color="#1874CD"]_s[/color] [color="#191970"][b]select[/b][/color] [color="#8B3E2F"][b][[/b][/color][color="#1874CD"]_i[/color] [color="#8B3E2F"][b]+[/b][/color] [color="#191970"][b]count[/b][/color] [color="#1874CD"]_f[/color][color="#8B3E2F"][b]][/b][/color][color="#8B3E2F"][b])[/b][/color]
[color="#8B3E2F"][b]}[/b][/color][color="#8B3E2F"][b];[/b][/color]

[color="#191970"][b]hint[/b][/color] [color="#8B3E2F"][b]([/b][/color][color="#8B3E2F"][b][[/b][/color][color="#7A7A7A"]"hello world"[/color][color="#8B3E2F"][b],[/b][/color] [color="#7A7A7A"]"hello"[/color][color="#8B3E2F"][b],[/b][/color] [color="#7A7A7A"]"good bye"[/color][color="#8B3E2F"][b]][/b][/color] [color="#191970"][b]call[/b][/color] KK_fnc_strReplace[color="#8B3E2F"][b])[/b][/color][color="#8B3E2F"][b];[/b][/color][/color]

Made with KK's SQF to BBCode Converter

Share this post


Link to post
Share on other sites

That's so much shorter than what I wasted my time on :P. Another question off topic now, what are the benefits when using private [...] in this particular example? It would work without the private [...] too? Is there some kind of performance gain or formality tied to it?

Share this post


Link to post
Share on other sites

private limits and isolates variable in scope it was defined in

Edited by Killzone_Kid

Share this post


Link to post
Share on other sites

My solution is exactly the same as KK's...

DE_replaceSection =
{
_input = _this select 0;
_replace = _this select 1;
_replaceWith = _this select 2;
_output = "input not found";

_location = _input find _replace;

if (_location > -1) then
{
	_front = _input select [0, _location];
	_end = _input select [_location + (count _replace)];
	_output = format ["%1%2%3", _front, _replaceWith, _end];
};
_output;
};

hint str (["Hello! My name is DreadedEntity", "DreadedEntity", "Ed"] call DE_replaceSection);

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×