Hi there.
I'm struggling to remove null/empty elements from a string array. I can't find a way to remove such elements without sorting or converting to unique set. I will appreciate any hints how can I do the above operation.
Example code:
string source_string = "testelem1,,testelem2,,,testelem3,testelem2"; string[] my_array = split(source_string, ",");
I have tried :
None of the above worked.
So far the I have found only two possibilities, both messing up order of elements in original array thus not fitting my needs:
Hi Blazej,
deleteElement actually works, but stops on first occurrence, so you'll need something like this:
number sz; do { sz = size(arr); arr = deleteElement(arr, ""); } while(sz != size(arr));
Another option would be to use arrayDiff
arr = arrayDiff(arr, {""});
Note: your "my_array" is declared as string not array.
Hope this helps!
Of course, I ate [] here :) arr = arrayDiff(arr, {""}); works perfectly! That's exactly what I've been looking for, thanks!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Of course I could just loop through the whole original array and copy all not null elements to another array, but it seems a bit overkill with large arrays :)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.