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

changing some characters in string

marcin_cholewczuk
Active Contributor
0 Likes
1,657

Hi all,

I want to change in string occurance of some unwanted characters with '_', but I know only good characters. Any idea how it can be done simply ?

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,560

hi,

*Code to Replace one char with another using TRANSLATE command

*----


DATA: ld_date(10) type c.

ld_date = '28.10.1979'.

*Finds all occurences of first char(.) & replaces them with second(-)

TRANSLATE ld_date using '.-'.

*ld_date will now contain '28-10-1979'

Regards,

Bijal

10 REPLIES 10
Read only

anversha_s
Active Contributor
0 Likes
1,560

hi,

DATA: int_char(10) type c value '0123456789'.

if var CO sy-abcde.
* only alphabets
elseif var CO int_char.
* only numberics
else.
Special cases.

you have to chk that <b>special cases</b>

Regards

Anver

Read only

0 Likes
1,560

uhhhhhh, but that was not my question. I want to <b>change</b> all occurance of bad characters with '_'.

Read only

Former Member
0 Likes
1,560

Hi,

DATA: String(20) type c value 'sautyn_nbj_jjk',
          string1(20) type c,
          str(1) type c value '_'.

REPLACE str with space INTO string1.
CONDENSE string1 NO-GAP.

Ok so u can

DATA: String(20) type c value 'sautyn@nbj,jjk'.

<b>SPLIT STRING AT DELIMITERS INTO str1 str2 str3...</b>

depending on number of delimiters.

Then

<b>CONCATENATE str1 str2 str3 INTO string SEPERATED BY '_'.</b>

Hope this helps.

Read only

Former Member
0 Likes
1,560

Marcin,

To replace 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.

DATA: T(10) VALUE 'abcdefghij',

STRING LIKE T,

STR1(4) VALUE 'cdef',

STR2(4) VALUE 'klmn'.

REPLACE STR1 WITH STR2 INTO STRING.

WRITE / STRING.

Pls. mark if useful

Read only

anversha_s
Active Contributor
0 Likes
1,560

hi,

* Replacing and translating characters in strings
DATA: STRING(80).

STRING = 'Variable_The variable _ is substituted later.'.

REPLACE '_' WITH ' ' INTO STRING.
condense string no-gaps.

WRITE / STRING.

Regards

Anver

Read only

Former Member
0 Likes
1,561

hi,

*Code to Replace one char with another using TRANSLATE command

*----


DATA: ld_date(10) type c.

ld_date = '28.10.1979'.

*Finds all occurences of first char(.) & replaces them with second(-)

TRANSLATE ld_date using '.-'.

*ld_date will now contain '28-10-1979'

Regards,

Bijal

Read only

Former Member
0 Likes
1,560

Hi,

lets say yout string name is STRING1. and i am giving the code for Numbers, if you want alphabets then chage the code according, only change the INT_CHAR to length 26 and values should be a to z.

DATA: int_char(10) type c value '0123456789'.
Data: len type i.
data: first type i, 
        last type i.
first = 0.
last = 1.
describe field STRING1 length LEN.
do LEN times.
 STRING+first(last) CO INT_CHAR.
 if sy-subrc <> 0.
 replace STRING1+first(last)  with '_' into STRING1.
 endif.
first = first + 1.
last = last + 1.
Enddo.

Regards

Sudheer

Message was edited by:

Sudheer Junnuthula

Read only

0 Likes
1,560

Hi

Sudheer your solution is what I thought of, but I was hoping that there is possibility to do this in simplier way. If I knew bad characters I would've done it like bijal proposed or by using OVERLAY instruction - that was kind of solution I was thinking about.

Read only

0 Likes
1,560

Hi,

In Sudheer's reply u can do like this

<b>DATA: int_char(30) type c value '`~!@#$%^&*()_-+=|\"':;?/>.<,'.</b>


Data: len type i.
data: first type i, 
        last type i.
first = 0.
last = 1.
describe field STRING1 length LEN.
do LEN times.
 STRING+first(last)

<b>CA</b>

INT_CHAR.
 if sy-subrc <> 0.
 replace STRING1+first(last)  with '_' into STRING1.
 endif.
first = first + 1.
last = last + 1.
Enddo.

Hope this helps. Include all unwanted characters in the int_char.

Read only

Former Member
0 Likes
1,560

Hi marcin,

if u have good characters in variables say v1 v2 v3.

concatenate v1 '_' v2 '_' v3 into str.

regards,

keerthi