2014 Feb 12 3:45 PM
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
2014 Feb 12 4:06 PM
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.
2014 Feb 12 3:57 PM
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.
2014 Feb 12 4:06 PM
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.
2014 Feb 12 4:07 PM
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.
----------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------
Best regards
2014 Feb 12 4:23 PM
Adrián Mejido and Bernat Loscos , thanks a lot .. your codes work perfectly
2014 Feb 12 5:22 PM
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.
/.