Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Length of a string

Former Member
0 Likes
2,128

Hello,

I am wondering what is the code to obtain the length of a string of characters, is there such a thing like sy-length? Thanks a lot!

Regards,

Anyi

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,259

Hi

DATA: STRING(20).

DATA: LEN TYPE I.

STRING = 'ABDCE'.

LEN = STRLEN( STRING ).

In this case LEN = 5.

Or you can use DESCRIBE FIELD statament to know the length of variable (in this case 20):

DESCRIBE FIELD STRING LENGTH LEN.

Max

6 REPLIES 6
Read only

Former Member
0 Likes
1,260

Hi

DATA: STRING(20).

DATA: LEN TYPE I.

STRING = 'ABDCE'.

LEN = STRLEN( STRING ).

In this case LEN = 5.

Or you can use DESCRIBE FIELD statament to know the length of variable (in this case 20):

DESCRIBE FIELD STRING LENGTH LEN.

Max

Read only

0 Likes
1,259

If you just need the <u>defined</u> length(as opposed to occupied length which STRLEN tells you) of a field then you can use the code below.

DESCRIBE FIELD myfield LENGTH field_length.

Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
1,259

You will need to use the STRLEN keyword.

data: len type i.
data: str type string.

str = 'This is the string that we want to measure'.

len = STRLEN( str ).

write:/  len.

Regards,

Rich Heilman

Read only

Former Member
0 Likes
1,259

Hi,

There is no System variable to find the length of the string.

We have to go with Strlen( ) as other have used.

Thanks,

Pramod

Read only

Former Member
0 Likes
1,259

H anyi,

The ABAP function STRLEN returns the length of a string up to the last character that is not a space.

[COMPUTE] <n> = STRLEN( <c> ).

STRLEN processes any operand <c> as a character data type, regardless of its real type. There is no type conversion.

As with mathematical functions, the keyword COMPUTE is optional.

DATA: INT TYPE I,

WORD1(20) VALUE '12345'.

WORD2(20).

WORD3(20) VALUE ' 4 '.

INT = STRLEN( WORD1 ). WRITE INT.

INT = STRLEN( WORD2 ). WRITE / INT.

INT = STRLEN( WORD3 ). WRITE / INT.

The results are 5 , 0, and 4 respectively.

reward helpful answers.

regards,

keerthi.

Read only

Former Member
0 Likes
1,259

hi anyi,

data: Str type string,

LEN type i.

len = strlen( str ).