Application Development 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: 

Dynamically adding text in Text Element.

karthick1608
Participant
0 Kudos

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

ArthurParisius
Contributor

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

ArthurParisius
Contributor

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.

0 Kudos

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!!

0 Kudos

Follow the recommendation of Arthur & use the message class.

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

0 Kudos

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.

0 Kudos

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.

@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.

0 Kudos

aparisius

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

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

former_member199306
Participant
0 Kudos

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.

0 Kudos

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

p244500
Active Contributor

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.