Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Dynamically adding text in Text Element.

karthick1608
Participant
0 Likes
11,298

Hello all,

I have very simple requirement, where I need to print a text in output using text element.

I created a text Element like

001 Text1 &1 Text2.

In the program I am using this text element inside a loop. When I try to use "WITH", it is resulting error.

loop .....

Write:/ text-001 with lv_var1.

Endloop

Kindly guide me to resolve this.

Thanks

1 ACCEPTED SOLUTION
Read only

ArthurParisius
Contributor
9,611

The "WITH" option only works if your using a message class and are giving messages.

For what you want to do I would place text-001 in a different variabel and then use REPLACE to change &1 with your other variable.

11 REPLIES 11
Read only

ArthurParisius
Contributor
9,612

The "WITH" option only works if your using a message class and are giving messages.

For what you want to do I would place text-001 in a different variabel and then use REPLACE to change &1 with your other variable.

Read only

0 Likes
9,611

Thanks for your reply!

I am new to ABAP, I know that we cannot use "WITH" in write statement.

For understanding purpose only I used like that.

Your idea is good and it will work, is there any other way without creating temporary variable, in your case?

Thanks again!!

Read only

0 Likes
9,611

Follow the recommendation of Arthur & use the message class.

Otherwise, create 2 texts and do a lv_result = text-001 && lv_value && text-002.

Read only

0 Likes
9,611

From the top of my head I would say split your text element in to 2 different elements. You could then use

write:/ text-001, lv_var1, text-002.
Read only

0 Likes
9,611

I recommend to avoid splitting the string into multiple text elements. This can cause problems during translation, as the variable position may differ in various languages. Either use a message class or the REPLACE statement.

Whenever I cannot use message classes for any reason I do something like this to emulate the behavior

message = message_with_symbols.
REPLACE FIRST OCCURRENCE OF '&' IN message WITH variable1. REPLACE FIRST OCCURRENCE OF '&' IN message WITH variable2.
...

REPLACE ALL OCCURRENCES OF '&1' IN message WITH variable1.
REPLACE ALL OCCURRENCES OF '&2' IN message WITH variable2.
Read only

9,611

@Gábor Márián

The only reason I suggested splitting was because he asked for a way to try and realize it without a temporary variable. Personally I would go with the temporary variable and the replace option, and the reason would most likely be to prevent issues with translations like you indicated.

Read only

0 Likes
9,611

aparisius

My intention was to emphasize that a temp variable today is better than a translation issue tomorrow 🙂

Read only

9,611
DATA(message) = replace( val = 'Hello &1 how are you doing?'(001) sub = '&1' with = 'Angela' ).
Read only

former_member199306
Participant
0 Likes
9,611

Hi Karthick,

if you want to print the value of text-001 and lv_var1 together then you can write -

lv_var = text-001 && lv_var1.

then print using write statement.

Read only

0 Likes
9,611

Hi Bhupendra,

Thanks for your response.

I am using dynamic variable in between the text symbol.

So, your idea wont work in my case.

Suggest if you have any other idea.

Thank you

Read only

Nawanandana
Active Contributor
9,611

Hi

Either use a Message class text or Replace as has been explained bellow.

Data : l_text type string.

l_text = TEXT-I01.

REPLACE '&1' WITH v_inr INTO l_text .

REPLACE '&2' WITH y_tbr INTO l_text.

CONDENSE l_text.