LEN function

Purpose: LEN returns the number of characters in a string.


Syntax:

 RetVal = LEN(MainStr AS STRING)

Return Value:

  • Data type: INTEGER
    RetVal Number of characters in MainStr.

Parameters:

  • Data type: STRING
    MainStr String to be measured for length.

Example:


 DIM a$
 DIM i%

 FOR i% = 65 TO 65+25
  a$ = a$ & CHR$(i%)
 NEXT

 i% = LEN(a$)

 ? "The length of a$ = " , i%

Result:


 The length of a$ =  26

BCX Console Sample Programs using LEN function.

S04.bas, S16.bas, S17.bas, S23.bas, S32.bas, S39.bas, S41.bas, S42.bas, S54.bas, S84.bas, S94.bas, S105.bas, S110.bas, S113.bas, S115.bas, S119.bas, S126.bas, S128.bas

MEMSIZE function

Purpose: MEMSIZE is an alias to the CRT function _msize. MEMSIZE is a case insensitive keyword that takes a dynamically dimensioned variable, that is, memory allocated using malloc, calloc, or realloc and returns its current memory allocation.


Syntax:

 RetVal = MEMSIZE(MainStr AS STRING)

Return Value:

  • Data type: INTEGER
    RetVal Size of the memory allocation for MainStr.

Parameters:

  • Data type: STRING
    MainStr A dynamic variable, dimensioned with malloc, calloc, or realloc.

Example:

See NEXTLINELEN below for an example of the usage of MEMSIZE.

LONGESTLINE function

Purpose: LONGESTLINE returns the length of the longest line in any text file, where each line of text is terminated with a CRLF.


Syntax:

 RetStr = LONGESTLINE(FileName AS STRING)

Return Value:

  • Data type: STRING
    RetStr The longest line in the file. If the file does not exist or cannot be opened for reading, the function will return zero.

Parameters:

  • Data type: STRING
    FileName The name of the file from which to retrieve the longest line.

Example:

The sample below will display the line(s) that equal the the length of the longest line in the file.


 DIM LL, A$
 LL = LONGESTLINE("bc.bas")
 OPEN "Bc.bas" FOR INPUT AS 1
 WHILE NOT EOF(1)
  LINE INPUT 1, A$
  IF LEN(A$) = LL THEN PRINT A$
 WEND
 CLOSE

Result:


          SecondVar$ = "Line" + STR$(LocalVars[s].VarLine) + " in Module: " + LocalVars[s].VarModule$ + " : " + LocalName$ + LocalVars[s].VarDim$ + " as " + GetVarTypeName$(LocalVars[s].VarType) + SPC$ + TypeDefs[LocalVars[s].VarDef].VarName$

NEXTLINELEN function

Purpose: NEXTLINELEN returns the length of the next line to be read from a currently open text file, where each line of text is terminated with a CRLF. This provides an opportunity to REDIM a receiving string variable before executing a LINE INPUT function, helping to prevent buffer overruns.


Syntax:

 RetVal = NEXTLINELEN(FileHandle AS FILE)

Return Value:

  • Data type: INTEGER
    RetVal Length of the next line in the file.

Parameters:

  • Data type: FILE
    FileHandle The handle of the file being processed.

Example:


 FUNCTION Fetchline(FP AS FILE, BYREF MyBuf$) AS UINT
  DIM LineLen AS UINT
  '========================== Query the next line length. 
  LineLen = NEXTLINELEN(FP) '<- Our new file function 
  '========================== REDIM MyBuf$ only when needed. 
  IF MEMSIZE(MyBuf$) < LineLen THEN REDIM MyBuf$ * LineLen
  LINE INPUT FP, MyBuf$
  FUNCTION = LineLen
 END FUNCTION