User:Eriksiers/TrimNonPrintables

From Rosetta Code

This is an extended version of BASIC's Trim/LTrim/RTrim set of functions. Where those three only handle spaces, this handles all non-printables in the base ASCII character set (ASCII 0-127), and treats all extended ASCII characters (128-254) as printable. (ASCII 255 is a non-printable blank.)

All BASIC code on this page is in the public domain.

Works with: QBasic
Works with: Visual Basic
Works with: PowerBASIC
FUNCTION LTrimNonPrintable$ (what AS STRING)
    DIM L0 AS LONG, found AS INTEGER
    FOR L0 = 1 TO LEN(what)
        SELECT CASE ASC(MID$(what, L0, 1))
            CASE 0 TO 32, 127, 255
                'trim
            CASE ELSE
                found = -1
                EXIT FOR
        END SELECT
    NEXT
    IF found THEN
        LTrimNonPrintable$ = MID$(what, L0)
    ELSE
        LTrimNonPrintable$ = ""
    END IF
END FUNCTION

FUNCTION RTrimNonPrintable$ (what AS STRING)
    DIM L0 AS LONG, found AS INTEGER
    FOR L0 = LEN(what) TO 1 STEP -1
        SELECT CASE ASC(MID$(what, L0, 1))
            CASE 0 TO 32, 127, 255
                'trim
            CASE ELSE
                found = -1
                EXIT FOR
        END SELECT
    NEXT
    IF found THEN
        RTrimNonPrintable$ = LEFT$(what, L0)
    ELSE
        RTrimNonPrintable$ = ""
    END IF
END FUNCTION

FUNCTION TrimNonPrintable$ (what AS STRING)
    TrimNonPrintable$ = LTrimNonPrintable$(RTrimNonPrintable$(what))
END FUNCTION