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

String Replace beside first character

Former Member
4,609

Hello,

I am new to ABAP and I need some help for a project.

I have some data (NAME1, NAME2), the value is a string for example John Doe.

I need to anonymize this data because its showing on some reports, so I want to make a check and take all the values and change them in J*** D** , i need 1st letter to stay unchanged and the rest of the characters to be changed to ' * ' .

I need to make this to a whole table with names.

I know that i need to use REPLACE IN WITH statement but i don't know how to keep the first character and change the others.

10 REPLIES 10
Read only

Abinathsiva
Active Contributor
0 Likes
3,946

Hi Bogdan,

String handling in ABAP link can help. Stings in SAP and String processing in SAP through ABAP

Regards,

Abinath

Read only

3,946

Found the answer, thank you

Read only

maheshpalavalli
Active Contributor
3,946

Try this.

REPLACE ALL OCCURRENCES OF REGEX '\B.' IN name1
                      WITH '*'.
or 

name1 = replace( val = name1 with = '*' regex = '\B.' occ = '0' ).
Read only

3,946

For information, the regular expression \B. means any character (.) which is inside the boundary characters of a word (\B).

Read only

irfanjazz1
Participant
0 Likes
3,946
data: name1 type char10,
      name2 type char10,
      v1 type char1,
      v2 type char1.


name1 = 'John'.
name2 = 'Deo'.
v1 = name1.
v2 = name2.

concatenate v1'***' v2'***' into name1.
break-point.

Read only

venkateswaran_k
Active Contributor
3,946

Hi

You can try this - I tried this - it works -

name1 = 'ALLEXCEPTFIRSTCHAR'.
mask_length = strlen( my_var ) - 1.    
name1 = replace( val = name1
                  off = 1
                  len = mask_length
                  with = repeat( val = '*' occ = mask_length ) ).

I tested it and it is working as expected.

Read only

0 Likes
3,946

Hi

Hope this works for you

Read only

0 Likes
3,946
DATA : var1 type string VALUE 'John Doe'
      , var2 TYPE string
       , varfinal TYPE char32
       , varfinal1 TYPE char32
       , varfinal2 TYPE char32
       , str TYPE char32
       , len TYPE i
       , len1 type i.

SPLIT var1 at space INTO : data(str1) data(str2)  .
len = strlen( str1 ).
len1 = len - 1.
var2 = repeat( val = '*' occ = len1 ).

CONCATENATE str1(1) var2 into varfinal1.
*write : ' Anonymous Text is :' , varfinal1.

len = strlen( str2 ).
len1 = len - 1.
var2 = repeat( val = '*' occ = len1 ).
CONCATENATE str2(1) var2 into varfinal2.
*write : ' Anonymous Text is :' , varfinal2.
CONCATENATE varfinal1 varfinal2 INTO varfinal SEPARATED BY space.
write : ' Anonymous Text is :' , varfinal.
Read only

0 Likes
3,946

result is :

Anonymous Text is : J*** D**

Read only

venkateswaran_k
Active Contributor
0 Likes
3,946

Hi

If you find the answer, please mark the right one and close the thread. Curios to know that