Current Poll
Quick overview of various ways to build a string using different options, like CONCATENATE, string offset, using a type.
Using &&
DATA: v_var1 TYPE char30, v_var2 TYPE char30, v_var3 TYPE char30. DATA: lv_result TYPE string. DATA: lv_char TYPE char100. v_var1 = 'Building'. v_var2 = 'A'. v_var3 = 'String'. *=== && * Available from release ABAP 731 lv_result = v_var1 && v_var2 && v_var3. WRITE: /(30) 'Using &&', lv_result.
Using String Templates
*=== String templates * Available from release ABAP 731 lv_result = |{ v_var1 }| & |{ v_var2 }| & |{ v_var3 }|. write: /(30) 'Using String templates', lv_result.
Using CONCATENATE
*=== concatenate CONCATENATE v_var1 v_var2 v_var3 INTO lv_result. WRITE: /(30) 'Using CONCATENATE', lv_result.
Using Type
*=== type TYPES: BEGIN OF lty_result, var1 TYPE char30, var2 TYPE char30, var3 TYPE char30, END OF lty_result. DATA: ls_result TYPE lty_result. ls_result-var1 = v_var1. ls_result-var2 = v_var2. ls_result-var3 = v_var3. * don't condense if respective blanks is required lv_result = ls_result. CONDENSE lv_result NO-GAPS. WRITE: /(30) 'Using Type', lv_result.
Using String Offset
*=== complicated PERFORM f_concatenate USING: v_var1 CHANGING lv_char, v_var2 CHANGING lv_char, v_var3 CHANGING lv_char. WRITE: /(30) 'Using Complicated', lv_char. *&---------------------------------------------------------------------* *& Form f_concatenate *&---------------------------------------------------------------------* FORM f_concatenate USING iv_input TYPE any CHANGING cv_var TYPE char100. DATA: lv_len TYPE i. lv_len = strlen( cv_var ). cv_var+lv_len = iv_input. ENDFORM. "f_concatenate
Let me know if you have any other way of building a string.
Do you have a Code Snippet which you want to share, Submit Code Snippet here
Hi Namesh,
I think string templates should be added to the list as additional possibility.
Keep up your good work!
Kind regards,
Valentin
There is also another way (since NW 7.3):
Zevolving ABAP Syntax Highlighter
Hello Valentin & Martin,
I totally forgot about the string templates and the chaining operator (&), even though I wrote articles on that in past
ABAP String Templates New Feature in ABAP 731
Difference Between && (Chaining Operator) and & (Literal Operator)
I guess, I’m not using them as much as I use other forms ๐
I have updated the code snippet with the string template as well.
Thanks,
Naimesh Patel
Naimesh,
String templates are very useful.
You don’t need to create three variables like var1, var2 and var3 and put it into a string (like in concatenate).
I know. String templates seem to be difficult to learn but it’s the simplest and shortest way to write short code without large blocks of “DATA:” ๐
Regards,
Marcin
Great! Just used the string template the other week!
@martin- I have been using that style since we upgraded last year.
Martin,
We still need to use variables if the text is not hardcoded. And most of the time, I make sure the text is not hard coded. Either it is from the Language dependent text table or from text elements – so I can have multilingual support. ๐
Thanks,
Naimesh patel
I always use string templates since I learnt about them in your blog. In you example, I would put it this way:
lv_result = |{ v_var1 } { v_var2 } { v_var3 }|.
When combined with REGEX and string functions you can do things like this, in one line:
l_output = |{ substring( val = SND_ID off = find( val = SND_ID regex = ‘(.{0,8})$’ ) ) }{ sy-uzeit }|.
Beautiful, isn’t it? ๐
Aurelio – It looks great! I guess your co-workers would have hard time to understand that if they don’t know what is string templates ๐
Hi Naimesh,
I think the example using string templates can be simplified:
This way you don’t have to use the concatenation operator and one can use string literals quite easily, e.g.:
Hello Valentin,
I believe there is difference when you use the Literal operator vs when you don’t use with the string template.
Without chaining operator there is a SPACE in between the literals where as when using with chaining operator &, string doesn’t have space within.
Thanks,
Naimesh Patel
Even more complicated:
Pablo,
I believe the syntax is from ABAP 740 release. Yes, I agree with you, it would be more complicated syntax to read but would be easy to write and look cleaner!