Skip to Content
-4

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

Feb 08, 2017 at 02:50 AM

315

avatar image

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 ?

10 |10000 characters needed characters left characters exceeded

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

0

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.

0

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

1

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

0

For education purpose only

0
Further commenting has been locked.
* Please Login or Register to Answer, Follow or Comment.

4 Answers

Best Answer
Aniruddha Chatterjee Apr 04, 2017 at 03:08 PM
1

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

Show 1 Share
10 |10000 characters needed characters left characters exceeded

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

2
Further commenting has been locked.
Horst Keller
Mar 25, 2017 at 06:48 PM
7

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?

Show 1 Share
10 |10000 characters needed characters left characters exceeded

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

0
Further commenting has been locked.
Richard Harper Mar 27, 2017 at 07:19 AM
5

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

Show 2 Share
10 |10000 characters needed characters left characters exceeded

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

0

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

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

1
Further commenting has been locked.
Horst Keller
Apr 04, 2017 at 04:18 AM
1

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

Show 1 Share
10 |10000 characters needed characters left characters exceeded

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

0
Further commenting has been locked.