Q(uick)BASIC Function: LTRIM$
Quick View
LTRIM$
A function that returns a copy of a string with leading spaces removed
Worth knowing
Useful and cross-version information about the programming environments of QBasic and QuickBasic.
Syntax
- LTRIM$(stringexpression$)
 - RTRIM$(stringexpression$)
 
Description/Parameter(s)
| stringexpression$ | Any string expression. | 
Example
a$ = "    Basic    "
PRINT "*" + a$ + "*"            'Output is:  *    Basic    *
PRINT "*" + LTRIM$(a$) + "*"    'Output is:  *Basic    *
PRINT "*" + RTRIM$(a$) + "*"    'Output is:  *    Basic*
              Syntax
- LTRIM$(stringexpression)
 
Description/Parameter(s)
The stringexpression can be any string expression.
Example
This example copies a file to a new file, removing all leading and trailing spaces.
Note: To run this example, you must supply the name of an existing text file.
CLS                          ' Clear screen
' Get the file names.
INPUT "Enter input file name:", InFile$
INPUT "Enter output file name:", OutFile$
OPEN InFile$ FOR INPUT AS #1
OPEN OutFile$ FOR OUTPUT AS #2
' Read, trim, and write each line.
DO WHILE NOT EOF(1)
   LINE INPUT #1, LineIn$
   ' Remove leading and trailing blanks.
   LineIn$ = LTRIM$(RTRIM$(LineIn$))
   PRINT #2, LineIn$
LOOP
CLOSE #1, #2
END
              See also:
Syntax
- LTRIM$(stringexpression$)
 
Description/Parameter(s)
stringexpression$ can be any string expression.
Example
This example uses the STR$ function to convert a number to its string representation and the LTRIM$ and RTRIM$ functions to strip out the leading and trailing blanks that BASIC ordinarily prints with numeric output.
CLS                            'Clear the screen.
PRINT "Enter 0 to end."
DO
    INPUT "Find cosine of: ", Num
    IF Num = 0 THEN EXIT DO
    X$ = STR$(Num)
    NumRemBlanks$ = LTRIM$(RTRIM$(X$))
    PRINT "COS("; NumRemBlanks$; ") = "; COS(Num)
LOOP
              Sample Output:
Enter 0 to end. Find cosine of: 3.1 COS(3.1) = -.9991351 Find cosine of: 0