• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

string_digits but it doesn't remove - and .

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

Summary:
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.

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

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

Code:
///string_digits_ext(str)
/*
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 -
    // https://i.stack.imgur.com/bwCFl.gif (Char list)
    if (nextOrd >= 48 and nextOrd <= 57) or nextOrd == 46 or nextOrd == 45
        {
        // Approve this character
        newStr += chr(nextOrd); // (Convert ord to char).
        }

    i++;
    }

// 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,
 

gnysek

Member
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.
 

Kezarus

Endless Game Maker
Gonna necro this post to say 2 things. Thanks @Pelican Police, that was exactly what I needed. And I made it for GMS 2.x too. =]

GML:
///string_digits_ext(str)
/*
Same as string_digits but also allows for . and -
Usefull if you know for sure there is a number in the variable
and you just want to retrieve it, but it doens't validate the number

Original Author: Pelican Police
*/
function string_digits_ext(str){
    var newStr  = ""; // The string we will return
    
    // Loop for each char in the string
    var len = string_length(str)
    for(var i=1; i <= len; i++){
        // 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 -
        // https://i.stack.imgur.com/bwCFl.gif (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 newStr;
}
GML:
function UnitTest_string_digits_ext(){
    //SETUP
    var result, expected;
    
    //TEST 1
    result = string_digits_ext("1");
    expected = "1";
    if( result != expected ){ throw("Error on UnitTest_string_digits_ext 1"); }
    //TEST 2
    result = string_digits_ext("-1.2");
    expected = "-1.2";
    if( result != expected ){ throw("Error on UnitTest_string_digits_ext 2"); }
    //TEST 3
    result = string_digits_ext("aaa-1.2bbb");
    expected = "-1.2";
    if( result != expected ){ throw("Error on UnitTest_string_digits_ext 3"); }
    //TEST 4
    result = string_digits_ext("-1.2bbb");
    expected = "-1.2";
    if( result != expected ){ throw("Error on UnitTest_string_digits_ext 4"); }
    //TEST 5
    result = string_digits_ext("aaa-1.2");
    expected = "-1.2";
    if( result != expected ){ throw("Error on UnitTest_string_digits_ext 5"); }
}

Cheers!
 
Top