Various Ways to Build a String

By | Last Updated on March 26, 2014 | 5,300

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

Share It!

Don't miss an Update

Get notified of the new post, right into your inbox

Naimesh Patel{274 articles}

I'm SAP ABAP Consultant for more than a decade. I like to experiment with ABAP especially OO. I have been SDN Top Contributor.
Follow :

Explore all of his 274 articles.

Load comments

12 Comments

  • Valentin

    Hi Namesh,
    I think string templates should be added to the list as additional possibility.

    Keep up your good work!

    Kind regards,
    Valentin

  • Martin

    There is also another way (since NW 7.3):

    Zevolving ABAP Syntax Highlighter

     
     
    DATA lv_string TYPE string.
     
    lv_string = |Hello | && |world!|.
     
    WRITE lv_string.
     
     
  • 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

  • Martin

    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

  • steve oldner

    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

  • Aurelio Hernรกndez

    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 ๐Ÿ˜‰

  • Valentin

    Hi Naimesh,

    I think the example using string templates can be simplified:

     
    lv_result = |{ v_var1 } { v_var2 } { v_var3 }|.
     

    This way you don’t have to use the concatenation operator and one can use string literals quite easily, e.g.:

     
    lv_result = |The result of adding { v_var1 } to { v_var2 } is { v_var3 }.|.
     
  • Hello Valentin,

    I believe there is difference when you use the Literal operator vs when you don’t use with the string template.

     
    *=== String templates
    lv_result = |{ v_var1 } { v_var2 } { v_var3 }|.
    write: /(30) 'String templates - No &', lv_result.
     
    *=== String templates
    lv_result = |{ v_var1 }|  & |{ v_var2 }| & |{ v_var3 }|.
    write: /(30) 'String templates - with &', lv_result.
     
    ** Output
    *String templates - No &        Building A String
    *String templates - with &      BuildingAString
     

    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

  • Pablo

    Even more complicated:

     
    lv_result = insert(  val = insert( val = v_var1 sub = v_var2 off = strlen( v_var1 ) ) sub = v_var3 off = strlen( v_var1 ) + strlen( v_var2 ) ).
     
  • 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!

Comments on this Post are now closed. If you have something important to share, you can always contact me.