Skip to Content
-4

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

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 ?

Add comment
10|10000 characters needed characters exceeded

Further commenting has been locked.
  • Get RSS Feed

4 Answers

  • Best Answer
    avatar image
    Former Member
    Apr 04, 2017 at 03:08 PM

    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

    Add comment
    10|10000 characters needed characters exceeded

    Further commenting has been locked.
  • Mar 25, 2017 at 06:48 PM

    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?

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member

      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

    Further commenting has been locked.
  • Mar 27, 2017 at 07:19 AM

    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

    Add comment
    10|10000 characters needed characters exceeded

    Further commenting has been locked.
  • Apr 04, 2017 at 04:18 AM

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

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member

      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

    Further commenting has been locked.