string_digits but it doesn't remove - and .

GM Version: 1.4
Target Platform: ALL
Download: string_digits_ext.gml
Links: N/A

The built in function string_digits is useful if you want to extract a number from a string. But it overlooks the fact that numbers also use the characters " - " and " . " and still removes these. The script below does exactly what string_digits does, but also leaves in those extra characters.

built in: string_digits("Your score is -17.09"); gives "1709"
this script: string_digits_ext("Your score is -17.09"); gives "-17.09"

- Download from link above, and then drag "string_digits_ext.gml" over your project window, then select "script" from the popup.
- Copy paste this code directly into a new script in your project:

Same as string_digits but also allows for . and -

// Arguments
var str     = argument0;
var newStr  = ""; // The string we will return

// Loop for each char in the string
var i = 1;
repeat string_length(str)
    // Retrieves the next char in the string (but as its ascii id)
    var nextOrd = string_ord_at(str,i);

    // If the char is from 0-9, is . or is -
    // (Char list)
    if (nextOrd >= 48 and nextOrd <= 57) or nextOrd == 46 or nextOrd == 45
        // Approve this character
        newStr += chr(nextOrd); // (Convert ord to char).


// Return a string of approved characters
return newStr;
I hope this can be of use to you!

Some more keywords:
Decimal separator, digits, values, int, integer, minus, dash, point, removes, deletes,
  • Like
Reactions: VX5


GMS function string_digit is named like this for purpose. It will return all digits in string. In case that we have "Maybe it's 4 maybe it's 2.5", we have two numbers here, but three digits. So, string_digit will return 425, and your function will return 42.5. If we want to get numbers from that string, it should return either array with 4 and 2.5, or only first. However, parsing it properly isn't an easy task, as we also need to take into account things like "-." or ".-5-.1--.". In fact, I have no idea what string like this should return, probably empty string as invalid number.

This is a good step trough getting numbers from string, and it will work great if you're sure, that there's only one valid number in checked string. It might return weird results if working on user input.
"Maybe it's 4 maybe it's 2.5" ... string_digit will return 425, and your function will return 42.5.
If you're looking to find more than one number, you wouldn't be using string_digits anyway. You'd use a custom script as you said. You'd use this script if you're looking for a single number, for example a number textbox on a UI panel.

we also need to take into account things like "-." or ".-5-.1--."
This is a great point, with a clear solution:
real(string_digits_ext( str ));
If symbols are in the wrong place, it will default to 0.

But this is a deliberate choice not to include this in the script, because if a user enters something bad in a textbox, I wouldn't want to just erase it. I'd prefer the textbox to go red so they can see the problem and amend it, rather than potentially be frustrated and not understand why their input was just reset.