Global::str2con() converts strings to numbers, if it can.

I don’t like that the function Global::str2con(str _value, str 10 _sep = ‘,’) converts strings to numbers, if it can. It does this on a field by field basis, i.e. the resulting container might be a mixed bag of strings and numbers, which is difficult to process.

I added my own function to avoid this:

public static container str2strCon(str _value, str 10 _sep = ',')
{
    #define.SC("¶") //special character (for reversibility of strCon2str with str2strCon)
    int length = strlen(_value), sepLength = strlen(_sep);
    int i = 1;
    int j = strscan(_value, _sep, 1, length);
    container ret;
    ;
    while (j)
    {
        ret += strReplace(substr(_value, i, j-i),#SC,_sep);
        i = j+sepLength;
        j = strscan(_value, _sep, i, length);
    }
    ret += strReplace(substr(_value, i, length-i+1)),#SC,_sep);
    return ret;
}

While we’re at it:
The reverse function con2str does not guarantee reversibility – if a component of the container contains the separator, then the reversing function (str2Con) will result in a larger container. So I added a similar function strCon2str() to generate strings from a container while removing the separator character from strings in a container. This guarantees the reversibility of the function. My str2strCon() function above restores the separator character in the component-strings.

static str strCon2str(container c, str 10 _sep = ',')
{
    #define.SC("¶") //special character (for reversibility of strCon2str with str2strCon)
    str ret, s;
    int i;
    ;
    for (i=1; i<= conlen(c); i++)
    {
        s = conpeek(c,i);
        ret = (ret?ret + _sep:"") + strreplace(s,_sep,#SC);
    }
    return ret;
}
Advertisements