2017 Feb 08 2:50 AM
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 ?
2017 Apr 04 4: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
2017 Feb 08 2:57 AM
May I know the reason for using custom logic over in-build one?
2017 Feb 08 4:40 AM
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.
2017 Mar 25 6: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?
2017 Apr 04 4:11 PM
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
2017 Mar 26 10:02 AM
Your question is not clear: which problem do you exactly have? (provide code, and include the types of your data objects)
2017 Mar 26 10:05 AM
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
2017 Mar 27 8: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
2017 Mar 27 9:06 AM
2017 Mar 27 10:36 AM
Love it. Reminds of the "hello, world" written by beginner to advanced:
2017 Apr 04 4:05 AM
2017 Apr 04 5:18 AM
ABAP is implemented in C/C++ in the Kernel, strictly internal, ...
2017 Apr 04 4:09 PM
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
2017 Apr 04 4: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
2017 Apr 04 8:58 PM
Run it. If it works, you know you've done it right. But now I'm closing the question.