Finding the length of a string in LANSA

Date:Archived
Product/Release:LANSA for the AS/400 and LANSA for Windows
Abstract:A couple of different ways to find out how many characters there are in a string
Submitted By:LANSA Technical Support

To find the length of a string the user could:

  1. Build a LANSA function/subroutine that processes a string right to left to find the last non blank character. This will return the length of the string.
    OR
  2. Write a built in function that does the same thing.

    However there is a better way…
  3. In LANSA there are already a number of built in functions (BIFs) supplied that work on strings eg CONCAT, BCONCAT, TCONCAT. These BIFs all have an optional parameter returned containing the length of the string. So, one of these BIFs could be used to find the length of a string, as shown in the following RDML function:-
********** COMMENT(Copyright..: (C) = LANSA TIP SYSTEM =, 1997);
********** COMMENT(Process .......:TIPS);
********** COMMENT(Function......: STRLEN);
********** COMMENT(Description...: How long is that string?);
DEFINE FIELD(#STRINGIN) TYPE(*CHAR)LENGTH(60) LABEL('Input String');
DEFINE FIELD(#EMPTYSTR) TYPE(*CHAR)LENGTH(1);
DEFINE FIELD(#STRLEN) TYPE(*DEC)LENGTH(3) DECIMALS(0) 
	     LABEL('String Length') EDIT_CODE(3);
********** COMMENT(Program Mainline: STRLEN);
FUNCTION OPTIONS(*DIRECT);
BEGIN_LOOP;
REQUEST FIELDS(#STRINGIN);
USE BUILTIN(TCONCAT) WITH_ARGS(#STRINGIN #EMPTYSTR) 
                        TO_GET(#STRINGIN #STRLEN);
DISPLAY FIELDS(#STRINGIN #STRLEN);
END_LOOP;