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

insert a '#' before every character.

Former Member
0 Likes
2,119

hi all,

i want to insert # symbol before every character in my string. but only if the character is not a * .

eg:    input =  hello           should give me

        output = #h#e#l#l#o

          input = *.hello        should give me

          output = *#.#h#e#l#l#o

  

How can this be coded. Thanks in advance

1 ACCEPTED SOLUTION
Read only

bernat_loscos
Explorer
0 Likes
2,025

Hi Rojer, you can check this code.

DATA: l_times      TYPE numc4,
       l_index      TYPE numc4,
       l_string     TYPE string,
       l_new_string TYPE string,
       l_char.

l_string = '*.hello'.

l_times = STRLEN( l_string ).

DO l_times TIMES.

   l_char = l_string+l_index(1).

   IF l_char NE '*'.
     CONCATENATE l_new_string '#' l_char INTO l_new_string.
   ELSE.
     CONCATENATE l_new_string l_char INTO l_new_string.
   ENDIF.

   l_index = l_index + 1.

ENDDO.

Regard.

5 REPLIES 5
Read only

Former Member
0 Likes
2,025

data : lv_length type i.

constants : lv_hash(1) VALUE '#'.

lv_length = strlen( input ).

DO lv_length times varying lv_char first input(1) next input(2).

iF lv_char <> '*'. .

CONCATENATE lv_string lv_hash lv_char INTO lv_string.

ENDIF.

ENDDO.

Read only

bernat_loscos
Explorer
0 Likes
2,026

Hi Rojer, you can check this code.

DATA: l_times      TYPE numc4,
       l_index      TYPE numc4,
       l_string     TYPE string,
       l_new_string TYPE string,
       l_char.

l_string = '*.hello'.

l_times = STRLEN( l_string ).

DO l_times TIMES.

   l_char = l_string+l_index(1).

   IF l_char NE '*'.
     CONCATENATE l_new_string '#' l_char INTO l_new_string.
   ELSE.
     CONCATENATE l_new_string l_char INTO l_new_string.
   ENDIF.

   l_index = l_index + 1.

ENDDO.

Regard.

Read only

adrian_mejido
Contributor
0 Likes
2,024

Hi Rojer,

This code will help you:

data: l_string TYPE string VALUE '*.HELLO',
        l_char VALUE '#',
        l_ast VALUE '*',
        l_string_aux TYPE String,
        l_longitud TYPE i,
        l_counter TYPE i.


l_longitud = strlen( l_string ).



** '*HELLO'
WHILE l_counter <= ( l_longitud - 1 ).

   IF l_string+l_counter(1) NE l_ast.
     CONCATENATE l_string_aux l_char l_string+l_counter(1) INTO l_string_aux.
   ELSE.
     CONCATENATE l_string_aux l_string+l_counter(1) INTO l_string_aux.
   ENDIF.

   l_counter = l_counter + 1.


ENDWHILE.

WRITE l_string_aux.



----------------------------------------------------------------------------------------------

----------------------------------------------------------------------------------------------


  • Example with string = '*.HELLO'.


              



  • Example with string = 'HELLO'.


              

Best regards


Read only

Former Member
0 Likes
2,024

Adrián Mejido and Bernat Loscos , thanks a lot .. your codes work perfectly 

Read only

Former Member
0 Likes
2,024

Here is a one-liner regex way to do it. Isn't that smooth?


DATA lv_str TYPE string VALUE '*.hello'.

REPLACE ALL OCCURRENCES OF REGEX '([^*])' IN lv_str WITH '#$1'.

WRITE lv_str.

/.