User:Eriksiers/TrimNonPrintables: Difference between revisions

From Rosetta Code
Content added Content deleted
(created)
 
m (updated lang tag)
 
Line 7: Line 7:
{{works with|PowerBASIC}}
{{works with|PowerBASIC}}


<lang qbasic>FUNCTION LTrimNonPrintable$ (what AS STRING)
<syntaxhighlight lang="qbasic">FUNCTION LTrimNonPrintable$ (what AS STRING)
DIM L0 AS LONG, found AS INTEGER
DIM L0 AS LONG, found AS INTEGER
FOR L0 = 1 TO LEN(what)
FOR L0 = 1 TO LEN(what)
Line 45: Line 45:
FUNCTION TrimNonPrintable$ (what AS STRING)
FUNCTION TrimNonPrintable$ (what AS STRING)
TrimNonPrintable$ = LTrimNonPrintable$(RTrimNonPrintable$(what))
TrimNonPrintable$ = LTrimNonPrintable$(RTrimNonPrintable$(what))
END FUNCTION</lang>
END FUNCTION</syntaxhighlight>

Latest revision as of 04:54, 1 September 2022

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