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

how to replace special characters in string

nirav_parghi4
Discoverer
0 Likes
26,391

Hi all.

I am having a problem that I am trying to replace special characters

in a string with hash. in 4.5 there is no function module to resolve this issue,

I have checked.

We do not know the complete set of special characters., even if we did,

I want a flexible way to replace the special characters in a string..

I tried one approach, to find special characters in string and

use offset to replace them with hash. but 4.5 does NOT allow

use of variable offset, only constant.

here is the code :

do total_length times.

   lw_position = sy-index - 1.

   concatenate 't_bkpf-xblnr+' lw_position '(1)'

               into lc_special_char.

   if lc_special_char na c_valid_chars.

     lw_special_char = lc_special_char.

     replace lw_special_char into lc_special_char

                           with lv_space.

   endif.

enddo.

Any solutions?

Thanks,

Regards,

Nirav

Hi all.

I am having a problem that I am trying to replace special characters

in a string with hash. in 4.5 there is no function module to resolve this issue,

I have checked.

We do not know the complete set of special characters., even if we did,

I want a flexible way to replace the special characters in a string..

I tried one approach, to find special characters in string and

use offset to replace them with hash. but 4.5 does NOT allow

use of variable offset, only constant.

here is the code :

do total_length times.

   lw_position = sy-index - 1.

   concatenate 't_bkpf-xblnr+' lw_position '(1)'

               into lc_special_char.

   if lc_special_char na c_valid_chars.

     lw_special_char = lc_special_char.

     replace lw_special_char into lc_special_char

                           with lv_space.

   endif.

enddo.

Any solutions?

Thanks,

Regards,

Nirav

15 REPLIES 15
Read only

Former Member
0 Likes
10,171

Hi,

Not sure to get the issue...

Why using that concatenate statement?

Why not simply:

IF t_bkpf-xblnr+lw_position(1) NA c_valid_char.

     REPLACE...

ENDIF.

Br,

Manu.

Read only

0 Likes
10,171

Please search for regular experisions/REGEX or check program dEMO_REGEX for the same

Nabheet

Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
10,171

Regular expressions are not supported in Release 4.5

Read only

0 Likes
10,171

Oops my fault overlooked it:)

Read only

0 Likes
10,171

Hi Manu,

I cannot use this because in t_bkpf-xblnr+lw_position(1)

I cannot use lw_position here since it is a variable. I can

only use constant in 4.5

Nirav

Read only

0 Likes
10,171

Too bad...

So the only way I can see so far is by shifting your string char by char (SHIFT ... LEFT BY 1 PLACES) and checking t_bkpf-xblnr+0(1)...

Cheers,

Manu.

Read only

arivazhagan_sivasamy
Active Contributor
0 Likes
10,171

Hi Nirav,

Please check as below.

   IF 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_' CS field+0(1).

  else.

  Write: / 'special characters are there'.

  endif.

You can check word by word.

Arivazhagan S

Read only

Former Member
0 Likes
10,171

Try like this..

DATA:string TYPE string.

string = './&^%&^%!@&%FARID^%^%@%!HASAN'.

REPLACE  ALL OCCURRENCES OF REGEX '[^[:alnum:]]' IN string WITH `#` .

WRITE string.

Read only

Former Member
0 Likes
10,171

Try Using Function module ES_REMOVE_SPECIAL_CHARACTER .

Read only

0 Likes
10,171

Guys...I have checked the 4.5 ..there is NO Function module to solve this...

Also I cannot use REGEX.

Any other solution?

Thanks,

Nirav

Read only

0 Likes
10,171

data : string  type string.
string = 'agkle*(&#kljabg23984903*%)(2k0j'.
data len type i.
data : ofs type i, c.

len = strlen( string ).
TRANSLATE string to UPPER CASE.
ofs = 0.
write : string.
do len times.
     c = string+ofs(1).
    if c CN '1234567890' and c CN sy-abcde.
      replace all OCCURRENCES OF c in string with '#' .
    endif.
    add 1 to ofs.
  enddo.
  write 😕 string.

Read only

Former Member
0 Likes
10,171

Hi,

pls check this

DATA:

       v_count TYPE i,

       v_namelen TYPE i,

       lv_variable TYPE string VALUE 'KIRAN!$%^'.

v_count = 0.

v_namelen = strlen( lv_variable ).

DO v_namelen TIMES.

   IF  lv_variable+v_count(1) NA sy-abcde ."or  lv_variable+v_count(1) NA space .

  REPLACE lv_variable+v_count(1) WITH '#'  INTO lv_variable.

   endif.

v_count = v_count + 1.

ENDDO.

Read only

nabheetscn
SAP Champion
SAP Champion
0 Likes
10,171

Hi Nirav

Can you please add here one example with your inputs and expected output..?

Nabheett

Read only

habrahams
Explorer
0 Likes
10,171

Hi Nirav,

You could potentially use the "SEARCH" statement to record all positions with special characters in a table, after which you could loop at the table and replace all positions with '#'.

This still does not seem like the best solutions, but maybe it should be considered.

Also have a look at the sap help documentation on string operations. You might find some good alternatives.

SAP Online Help

Read only

Kenber
Explorer
0 Likes
10,171

replace all occurrences of REGEX '(#)' in bseg-sgtxt with space.