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

Doubt on String Operations

Former Member
0 Likes
1,385

Hi,

I have a requirement in which i have to replace the character '|' with blank space. I have used the following command to replace.

Replace All occurrences of '|' in v_text1 with ' ' (Instead of Quotes i tried with SPACE option also) but it is not inserting blank space in between the characters.

Can anyone please help me out in this.

Thanks & Regards

Haritha.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,349

Hi,

Please use the TRANSLATE statement.

DATA text TYPE string.

text = `ABC|def|GHR|hgashdfk`.

TRANSLATE text USING '| '.

Output: ABC def GHR hgashdfk.

So TRANSLATE, replaces all the '|' with SPACE.

Cheers,

Srini.

14 REPLIES 14
Read only

former_member585060
Active Contributor
0 Likes
1,349

Hi,

Try below

REPLACE ALL OCCURRENCES OF '|' WITH ' '.   " give space using (Altkey+255)
                                                                          "using Alt key+255 it gives a single space

Regards

Bala Krishna

Read only

Former Member
0 Likes
1,349

Hi,

Thanks for the reply. Could you please put a sample code for that.

Thanks & Regards

Haritha.

Read only

0 Likes
1,349

Hi,

what balakrishna said will work perfectly fine

just try doing it like this

REPLACE ALL OCCURRENCES OF '|' IN w_string WITH ' '.

instead of pressing space bar, hold the alt key and press 255. it will automatically give you the space, its the ASCII key value of space (ALT + 255).

that will definitely work.

regards

sarves

Read only

0 Likes
1,349

HI,

Use the above syntax, instead of typing space bar for space use (Alt key+255) means Alt key on key board holding that Alt key press numbers 255, the remove finger from Alt key it will give u a space.

Regards

Bala Krishna

Read only

Former Member
0 Likes
1,349

Hi,

whats the data type of v_text1? try string. it works for me.

or try simply "Replace '|' into v_text1 with ' '." but it'l replace only once.

Read only

Former Member
0 Likes
1,349

hi

try out this code it works for me

data : a type string VALUE 'ABC|def|GHR|hgashdfk'.
data : c type string .
data : b type c VALUE '|'.
replace ALL OCCURRENCES of '|' in a with '  '. " as bala krishan told press the key alt and type 255 .

write : a.

regards

chinnaiya

Read only

Former Member
0 Likes
1,349

Hi,

There is another option also to do the same but bit lenghty:

DATA:Len type i,
         Counter type i,
         String1(10) type c value 'Iadsfdiadsfdifd',
         String2(10) type c.

Len = STRLEN ( String1 ).
Counter = 0.
while Countre LT Len.
if String1+Counter(1) EQ 'i'.
Concatenate String2 ' ' into String2.
else.
Concatenate String2  String1 into String2.
endif.
Write String2.

Hope this might help you out.

Pooja

Read only

Former Member
0 Likes
1,349

Hi Balakrishna,

I have tried the below code by using ALT+255, but it didnt gave me any space.

Data: v_text1 type string,

v_text2 type string.

v_text1 = 'ABC*DEF'.

move v_text1 to v_text2.

replace all occurrences of '*' in v_text2 with ''.

if sy-subrc = 0.

write:/ v_text1, v_text2.

endif.

Thanks & Regards

Haritha.

Read only

0 Likes
1,349

Hi Haritha,

ALT+255 will give a space, its ASCII format,

open a Notepad and write AB, place the cursor after A, and hold ALT key on keyboard, and press number 255 then remove finger from ALT key, you will see a space between A and B now.

Use same in the REPLACE statement to get space.

Regards

Bala Krishna

Read only

Former Member
0 Likes
1,349

Actually, after pressing the number 255, u have to hold the alt for a while and then release your finger. Try in numeric pad of keyboard.

Read only

Former Member
0 Likes
1,349

Hello Haritha,

I think the problem is the following: Character fields (data type c) cannot have blank spaces at the right side of the field. Thus, the character field ' ' which is used to replace the '|' character is actually ''.

If you use a string instead, the code should work:


DATA: gv_text1(20) TYPE c VALUE 'This|is|my|sample'.

* Replace | character with spaces
REPLACE ALL OCCURRENCES OF '|' IN gv_text1 WITH ` `.

WRITE gv_text1.

Regards

David

Read only

Former Member
0 Likes
1,350

Hi,

Please use the TRANSLATE statement.

DATA text TYPE string.

text = `ABC|def|GHR|hgashdfk`.

TRANSLATE text USING '| '.

Output: ABC def GHR hgashdfk.

So TRANSLATE, replaces all the '|' with SPACE.

Cheers,

Srini.

Read only

Former Member
0 Likes
1,349

Hi,

The below statement also works, however there is a twist.

REPLACE ALL OCCURRENCES OF '|' IN gv_text1 WITH ` `.

The space ` `, these characters are under the tilde sign on the keyboard, these are not the single quotes(see the difference, single quote ('), the other one is (`).

If you you ' ', the statement does not work.

so,

REPLACE ALL OCCURRENCES OF '|' IN gv_text1 WITH ' '. "Does not work:

Works:

REPLACE ALL OCCURRENCES OF '|' IN gv_text1 WITH ` `. " works

Hope you issue will be solved.

Read only

Former Member
0 Likes
1,349

Hi Srinivas,

Thanks a lot. Your logic of Translate command worked.

Hi Balakrishna,

I think the logic which you said works only in versions which are greater than 4.7, it didn't worked for me even i tried to do it in notepad. Anywayz thanks a lot for the suggestions given.

Thanks & Regards

Haritha.