Jump to content
Sign in to follow this  
floppy_pancakes

Script Release: Remove Whitespace

Recommended Posts

Not sure if this is the proper place to release such a script, but if not, just move it.

I needed a super simple file to remove whitespace from a string, and google found nothing. So if it exists, it's properly done better than mine. 

 

REGARDLESS! Here is mine in case anyone else needs it! Enjoy! :)

/*

	File: REV_fnc_removeWhitespace
	Author: Floppy Pancakes
	Usage: ["string here"] call REV_fnc_removeWhitespace
	Returns: "stringhere"
	License: My words. Use freely, leave credit. Simple. :)

*/
private ["_stringToParse"];

_originalString = _this select 0;
_stringToParse = toArray _originalString;

while {32 IN _stringToParse} do {
    _index = _stringToParse find 32;
    _stringToParse deleteAt _index;
};
_finalString = toString _stringToParse;

_finalString;

Share this post


Link to post
Share on other sites

maybe a noob question .. Ill be a noob for a life time..

but what does this do exactly?

 

please don't say it deletes white space ( lol ) I got that part, I wanna understand more.

Share this post


Link to post
Share on other sites

There is a simpler way:

_s = "string  with     spaces"; 
_s = _s splitString " " joinString ""; 
hint str _s;

Done.

  • Like 1

Share this post


Link to post
Share on other sites

There is a simpler way:

_s = "string  with     spaces"; 
_s = _s splitString " " joinString ""; 
hint str _s;

Done.

 

literally remembered about split and join a few minutes after I did it... :(

Hopefully my example will still be a good example for others in how they can accomplish other tasks. :)

Share this post


Link to post
Share on other sites

maybe a noob question .. Ill be a noob for a life time..

but what does this do exactly?

 

please don't say it deletes white space ( lol ) I got that part, I wanna understand more.

"If  your      string" had that in it say it was inputted by a  player for whatever reason or the script you were using outputted it like that, it will remove that space.

Share this post


Link to post
Share on other sites

literally remembered about split and join a few minutes after I did it... :(

Hopefully my example will still be a good example for others in how they can accomplish other tasks. :)

working code examples are always good for people that start with sqf. As I remember myself, I couldn't get enough of them ;)

Share this post


Link to post
Share on other sites

Seems kind of inefficient to loop through the input every cycle to see if you need to keep running, then loop through again to find the character you need to delete. If spaces are expected in your input it would be much better to simply iterate through it and scrub bad characters, rather than checking some condition. It would also allow you to use a for loop since you know the amount of times the loop needs to be cycled (good practice). Also, spaces are whitespace, however, it is not the only whitespace.

private "_noWhitespace";

_noWhitespace = toArray(_this);
{
	// if statement is false if character is not: space, tab, carriage return, newline
	if ([32, 9, 13, 10] find _x > -1) then
	{
		_noWhitespace deleteAt _forEachIndex;
	};
} forEach _noWhitespace;

toString(_noWhitespace);

Share this post


Link to post
Share on other sites

 

Also, spaces are whitespace, however, it is not the only whitespace.

good point

splitString toString [32,9,13,10] joinString ""

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  

×