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: 

String operation

Former Member
0 Kudos

Hi Abapers,

Please tell me how to replace first 4 characters of one variable with some other characters.

13 REPLIES 13

Former Member
0 Kudos

Hi,

Try using Replace statement.

regards,

Santosh Thorat

Former Member
0 Kudos

Hi,

You can use REPLACE ALL..

Ex..

REPLACE ALL OCCURRENCES OF ',' IN temp WITH ''.

Thanks

naren

Former Member

Former Member
0 Kudos

hi shruti,

a string in a field with a different string, use the REPLACE statement.

REPLACE <str1> WITH <str2> INTO <c> [LENGTH <l>].

The statement searches the field <c> for the first occurrence of the first <l> positions of the pattern <str1>. If no length is specified, it searches for the pattern <str1> in its full length.

Then, the statement replaces the first occurrence of the pattern <str1> in field <c> with the string <str2>. If a length <l> was specified, only the relevant part of the pattern is replaced.

If the return code value of the system field SY-SUBRC is set to 0, this indicates that <str1> was found in <c> and replaced by <str2>. A return code value other than 0 means that nothing was replaced. <str1>, <str2>, and <len> can be variables.

*************************

example:

DATA: T(10) VALUE 'abcdefghij',

STRING LIKE T,

STR1(4) VALUE 'cdef',

STR2(4) VALUE 'klmn',

STR3(2) VALUE 'kl',

STR4(6) VALUE 'klmnop',

LEN TYPE I VALUE 2.

STRING = T.

WRITE STRING.

REPLACE STR1 WITH STR2 INTO STRING.

WRITE / STRING.

STRING = T.

REPLACE STR1 WITH STR2 INTO STRING LENGTH LEN.

WRITE / STRING.

STRING = T.

REPLACE STR1 WITH STR3 INTO STRING.

WRITE / STRING.

STRING = T.

REPLACE STR1 WITH STR4 INTO STRING.

WRITE / STRING.

The output appears as follows:

abcdefghij

abklmnghij

abklmnefgh

abklghij

abklmnopgh

***********************

reward if useful..

Former Member
0 Kudos

Hi,

Check the mentioned examples you will get a clear picture.

*EG1:

DATA STR1(36).

STR1 = 'APPLE & ORANGE & BANANA'.

REPLACE '&' WITH ' . ' INTO STR1.

WRITE:/ STR1.

*EG2:

Data p(6) value 'ABCABC'.

Replace 'ABC' with 'DEF' into p.

Write:/ p.

Reward if helpful.

Regards,

Harini.S

Former Member
0 Kudos

I want to add 10 spaces to the variable.How to do this?

Former Member
0 Kudos

Hi Shruti,

Data: str1(10) type c value 'abcdefghij',

str2(4) type c value 'wxyz'.

shift str1 by 4 places.

concatenate str2 str1 into str1.

condense str1 no-gaps.

Former Member
0 Kudos

Hi,

Try this..

DATA: BEGIN OF S_TEST,

CHAR(10),

VALUE(10),

END OF S_TEST.

S_TEST-VALUE = 'TeST'.

**Now S_TEST will have 10 blank places in front..

Thanks

naren

rajesh_akarte2
Active Participant
0 Kudos

Hi Shruti ,

Go through this example.

DATA v1(10) TYPE C value '1234567891'.

REPLACE SECTION OFFSET 0 LENGTH 1 OF v1 WITH 'H'

IN CHARACTER MODE.

REPLACE SECTION OFFSET 1 LENGTH 1 OF v1 WITH 'H'

IN CHARACTER MODE.

REPLACE SECTION OFFSET 2 LENGTH 1 OF v1 WITH 'H'

IN CHARACTER MODE.

REPLACE SECTION OFFSET 3 LENGTH 1 OF v1 WITH 'H'

IN CHARACTER MODE.

WRITE: v1.

please reward the points if helpful.

Regards,

Rajesh Akarte

Former Member
0 Kudos

Ok. If the number of spaces I want to add is not a constant. Suppose some times 20 sometimes 21 sometimes 30. It depends on the value of PO number.

Former Member
0 Kudos

HI,

Try this..give the input and it will put that number of blank spaces in the front.


PARAMETERS: P_TIMES TYPE I.


START-OF-SELECTION.

DATA: V_STRING TYPE STRING.

**default value.
V_STRING= 'TEST'.

*" Concatenate blank according to the selectionscreen parameter.
do p_times times.

  concatenate space v_string into v_string.

enddo.

write: / v_string.

Thanks

naren

Former Member
0 Kudos

Thanks Naren. Your logic of taking a structure worked out.

Former Member
0 Kudos

You are welcome

Regards

Naren