‎2021 Jun 23 3:25 PM
data : text type string value 'rain'.
data: str type i.
str = STRLEN( text ).
data text2 type c LENGTH str.
text2 = text.
write : str.
write: text2.
I'M GETTING THIS ERROR : "STR" IS NOT A CONSTANT .
PLEASE HELP
‎2021 Jun 23 3:25 PM
Thank you for visiting SAP Community to get answers to your questions. Since this is your first question, I recommend that you familiarize yourself with: https://community.sap.com/resources/questions-and-answers, as the overview provides tips for preparing questions that draw responses from our members.
Should you wish, you can revise your question by selecting Actions, then Edit.
By adding a picture to your profile you encourage readers to respond: https://www.youtube.com/watch?v=46bt1juWUUM
Keep in mind, when you receive an answer that was helpful to you, accept it as
best answer.
Best,
Your SAP Community moderator
‎2021 Jun 23 3:46 PM
In ABAP, the length of character data should be specified statically. So, for your requirement, you should do like this. You should try like this.
DATA:
lv_string TYPE string,
lv_strlen TYPE i,
lv_char TYPE c LENGTH 255.
DATA:
lr_char TYPE REF TO data,
lo_descr TYPE REF TO cl_abap_elemdescr.
FIELD-SYMBOLS:
<fs_data> TYPE any.
"Assign value to string variable
lv_string = 'Hello word'.
lv_char = lv_string.If you still want to declare the length of char data type dynamically, it will more advance technic. You can use below code to achieve this. This uses the concept of data reference, RTTS and field symbol. If you are new to SAP no need to focus on these topic as these are pretty advance. So you can use the above code to specify the length of char data type statically.
DATA:
lv_string TYPE string,
lv_strlen TYPE i.
DATA:
lr_char TYPE REF TO data,
lo_descr TYPE REF TO cl_abap_elemdescr.
FIELD-SYMBOLS:
<fs_data> TYPE any.
"Assign value to string variable
lv_string = 'Hello word'.
"Get length of string variable
lv_strlen = strlen( lv_string ).
"Create a character data type of length of string
lo_descr = cl_abap_elemdescr=>get_c( p_length = lv_strlen ).
"Create variable -> Data referece
CREATE DATA lr_char TYPE HANDLE lo_descr.
"assign to field symbol
ASSIGN lr_char->* to <fs_data>.
"Assign value to new character varible
<fs_data> = lv_string.
"Print the value
WRITE:/ <fs_data>.<br>
‎2021 Jun 23 4:33 PM
The answer is in the error message.
Why don't you want to use a constant?