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: 

How to calculate length of string in abap programming language without using in-built function?

suba1994hit
Explorer
9,341

I am new in abap programming. I am facing some problems regarding string manipulation.

I had tried to calculate length of string without using in-built function like strlen ?

1 ACCEPTED SOLUTION

Former Member
4,173

Hello,

Please find my answer below.

**********************

*&---------------------------------------------------------------------*

*& Report ZTEST_STRLEN

*&---------------------------------------------------------------------*

REPORT ztest_strlen.

* selection screen
PARAMETER: gv_str TYPE string DEFAULT 'Hello World'.
* initialize
DATA(gv_offset) = 0.
DATA(gv_position) = 1.
* try for end of string
DO.
TRY.
DATA(gv_char) = gv_str+gv_offset(gv_position).
* catch if any exception
CATCH cx_root INTO DATA(lx_root).
IF lx_root IS NOT INITIAL.
DATA(lv_message) = lx_root->get_text( ).
ENDIF.
ENDTRY.
IF lv_message IS NOT INITIAL.
EXIT.
ELSE.
gv_offset = gv_offset + gv_position.
ENDIF.
ENDDO.
* output w/o STRLEN
WRITE:/ text-001, space, gv_offset.
* output using STRLEN
DATA(gv_len) = strlen( gv_str ).
WRITE:/ text-002, space, gv_len.

*****************************

BR,

Aniruddha

14 REPLIES 14

Former Member
0 Kudos
4,173

May I know the reason for using custom logic over in-build one?

suba1994hit
Explorer
0 Kudos
4,173

In C/C++/Java a string always ends with null or '\0'.

As a new learner, I want to know how the strlen function is working. It is just for educational purpose.

retired_member
Product and Topic Expert
Product and Topic Expert
4,173

Be aware that strlen doesn't count trailing blanks in text fields while for text strings it simply returns the string length including trailing blanks. The result is retrieved in the C kernel and as for everything that is done in the kernel, it is the result that counts and not how it is done. A way to do it yourself in ABAP would be to loop over substrings or offset/lengthes and count, but why?

0 Kudos
4,173

Hello Sir,

I have submitted my answer. I request you to have a look and correct me if I am wrong or my logic can be enhanced.

Thanking you in advance!

BR,

Aniruddha

Sandra_Rossi
Active Contributor
0 Kudos
4,173

Your question is not clear: which problem do you exactly have? (provide code, and include the types of your data objects)

Sandra_Rossi
Active Contributor
4,173

Here are some references about the handling of strings by the kernel (there's a "string header" for every string data object): Character String and Byte String Processing for Release 7.0, EhP2 - Management of short strings and Memory Requirement for Deep Data Objects

Former Member
4,173

Here's your custom logic.

Class How_Long Definition Final.
      Public Section.
             Methods: Get_Ball_of_String            Returning Value(Ball_Of_String) Type String,
                      Unravel_indeterminate_ length Returning Value(Ball_Of_String) Type String,
                      Cut_With_A_Pair_Of_Scissors   Importing i_Ball                Type String,
                                                    Returning Value(Bit_Of_String)  Type String,
                      Measure_Bit_With_Ruler        Importing i_Bit_Of_String       Type String
                                                    Returning Value(Length_mm)      Type Millimetres,
                      Turn_Ruler_Round,                              
                      Measure_Bit_With_Ruler        Importing i_Bit_Of_String       Type String
                                                    Returning Value(Length_Inches)  Type Inches.
EndClass.

😉

Rich

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Kudos
4,173

Fell off my chair laughing. You just made my (Mon)day.

4,173

Love it. Reminds of the "hello, world" written by beginner to advanced:

http://www.smart-jokes.org/programmer-evolution.html

suba1994hit
Explorer
0 Kudos
4,173

For education purpose only

retired_member
Product and Topic Expert
Product and Topic Expert
4,173

ABAP is implemented in C/C++ in the Kernel, strictly internal, ...

0 Kudos
4,173

Hello Sir,

I have submitted my answer. I request you to have a look and correct me if I am wrong or my logic can be enhanced.

Thanking you in advance!

BR,

Aniruddha

Former Member
4,174

Hello,

Please find my answer below.

**********************

*&---------------------------------------------------------------------*

*& Report ZTEST_STRLEN

*&---------------------------------------------------------------------*

REPORT ztest_strlen.

* selection screen
PARAMETER: gv_str TYPE string DEFAULT 'Hello World'.
* initialize
DATA(gv_offset) = 0.
DATA(gv_position) = 1.
* try for end of string
DO.
TRY.
DATA(gv_char) = gv_str+gv_offset(gv_position).
* catch if any exception
CATCH cx_root INTO DATA(lx_root).
IF lx_root IS NOT INITIAL.
DATA(lv_message) = lx_root->get_text( ).
ENDIF.
ENDTRY.
IF lv_message IS NOT INITIAL.
EXIT.
ELSE.
gv_offset = gv_offset + gv_position.
ENDIF.
ENDDO.
* output w/o STRLEN
WRITE:/ text-001, space, gv_offset.
* output using STRLEN
DATA(gv_len) = strlen( gv_str ).
WRITE:/ text-002, space, gv_len.

*****************************

BR,

Aniruddha

matt
Active Contributor
4,173

Run it. If it works, you know you've done it right. But now I'm closing the question.