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

Replace First Character with Space

Former Member
0 Likes
10,357

In BW I have a record coming in and wtihin the address field the first character contains a ! or #. This is causing the DSO not to activate due to invalid characters of ! or # as the first character.  I am trying to replace these characters with a space.  Any ideas?  Listed below I am trying to to determine if the source field contains a ! as the first character.  If this is true then I am replacing ! with a space in the REPLACE statement.  Still no luck so asking around for suggestions.  Thanks. 

 

DATA: VADDRESS LIKE SOURCE_FIELDS-ADDRESS.

IF SOURCE_FIELDS-ADDRESS CP '!'.

VADDRESS = SOURCE_FIELDS-ADDRESS.

REPLACE '!' WITH ' ' INTO VADDRESS.

RESULT = VADDRESS.

ELSE.

RESULT = SOURCE_FIELDS-ADDRESS.

ENDIF.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
5,349

The fix was created a FORM globally that replaces any hexidecimal characters 00-1F, which are not valid and also replacing the ! and # in the first field of the character.

FORM

*$*$ begin of 2nd part global - insert your code only below this line  *

FORM format_for_bw USING i_replace TYPE string

  CHANGING c_text_field TYPE c.

  DATA:  l_field_length TYPE i,

         l_index TYPE sy-index.

  FIELD-SYMBOLS <l_char> TYPE X.

*Hexadecimal character OO-1F are not valid in BW and therefore need to

*be replaced.

  l_field_length = STRLEN( c_text_field ).

  DO l_field_length TIMES.

*Retrieve the character's hex value

    l_index = sy-index - 1.

    assign c_text_field+l_index(1) to <l_char> casting.

*If the hex value is between 00 and 1F, replace with space

    IF <l_char> <= '1F'.

      c_text_field+l_index(1) = i_replace.

    ENDIF.

  ENDDO.

* Need to check the source field for any characters BW cannot handle

* ! as the first character, # in any position

  IF c_text_field(1) = '!'.

    SHIFT c_text_field LEFT DELETING LEADING '!'.

  ELSEIF c_text_field(1) = '#'.

    SHIFT c_text_field LEFT DELETING LEADING '#'.

  ENDIF.

ENDFORM.

Next for each text field that would have the potental of invalid characters apply the code below, which calls the FORM for cleanup of the characters. 

RESULT = SOURCE_FIELDS-LASTNAME.

    PERFORM format_for_bw USING SPACE

      CHANGING RESULT.

15 REPLIES 15
Read only

Former Member
0 Likes
5,349

you can use REPLACE FIRST OCCURRENCE

but if u got invalid character error then you can use T.CODE RSKC you pass

@! $~{}_-|[]`\# and execute after that u active your DSO

Read only

Former Member
0 Likes
5,349

Hi Mary,

Please use the below code.

REPLACE FIRST OCCURRENCE OF '!' SOURCE_FIELDS-ADDRESS WITH ''.

CONDENSE SOURCE_FIELDS-ADDRESS.

Hope its useful.

Regards,

Rajesh

Read only

0 Likes
5,349

Thanks for responding.  One thing to note not every field will begin with the character ! so I my intent was to search the character string for a ! and then if found do the REPLACE statement.  With the below code even though it doesn't say REPLACE FIRST OCCURRENCE it is still only replacing the first find.  But for some reason the string search is not recognizing the ! as a character so it doesn't find it.  My code works if I switch it to a 1 for the search.  See below.

Code

IF SOURCE_FIELDS-ADDRESS CA '1'

VADDR = SOURCE_FIELDS-ADDRESS.

REPLACE '1' WITH ' ' INTO VADDR1.

RESULT = VADDR1.

Here are the results from the test.

Source field:  115 WEST COLLEGE DRIVE1

Result:   15 WEST COLLEGE DRIVE1

Read only

Former Member
0 Likes
5,349

Hi,

Try to use TRANSLATE command.

vaddress = '!jake#jason'.

TRANSLATE vaddress USING '! # '.

WRITE:/ vaddress.

Regards,

Jake

Read only

0 Likes
5,349

Thanks for responding.  I tried you suggestion and it appears to only work if the ! is contained within the string but not as the first character.  One thing to note is not every field will have a ! as the first character so that is why I am applying the search string statement first and if an ! is found then apply the TRANSLATE option.  Plus it appears to replace every character of ! where my intent is to just replace the first character.   Below is my code and the results.  Thanks.

CODE 

IF SOURCE_FIELDS-CANDIDATECITY CA '!'.

VADDR2 = SOURCE_FIELDS-CANDIDATECITY.

TRANSLATE VADDR2 USING '! '.

WRITE:/ VADDR2.

RESULT = VADDR2.

TEST

Source:  115! WEST! COLLEGE

Result: 115  WEST  COLLEGE

Read only

Former Member
0 Likes
5,349

fieldname(1) = ' '.

will work too if the field is not Type string.

Regards Nico

Read only

0 Likes
5,349

Thanks for responding.  That seemed to almost work.  The catch is not all records will have the ! as the first character so I wanted to apply the search string to find an ! point.  The code is not recognizing ! as a character for the search.  See below.

Code

IF SOURCE_FIELDS-ADDR1  CA '!'     ** Here if I switch to a 1 it works.**

VADDR1 = SOURCE_FIELDS-ADDR1

VADDR1(1) = ' '.

RESULT = VADDR2.

TEST

Source:  115

Result:  15

Read only

0 Likes
5,349

Try using:

IF SOURCE_FIELDS-ADDR1(1) = '!'.

Read only

amy_king
Active Contributor
0 Likes
5,349

Hi Mary Jo,

You mentioned that the first character may contain a ! or a #. The pound character may not be an actual pound character but SAP's representation of a non-printing character, e.g., a tab. We've seen this issue when a user copies-and-pastes text from another document and unintentionally brings along non-printing characters.

We corrected the issue by running any field that may contain non-printing characters (in your case, the address field) through a subroutine to remove any invalid characters.

For your scenario, you could construct a string of valid characters, then check if vaddress begins with anything other than this set of valid characters (IF vaddress(1) NOT CO valid_characters) and clear vaddress(1) if non-valid characters are found.

Cheers,

Amy

Read only

Former Member
0 Likes
5,349

Thanks for responding.  I tried what you suggested and it appears that the search does not take into account if the first character has a ! in the field.  If I move the ! to the end of the field it acknowledges it and then replaces the first character with a space.  Below are the results of my test.

Source:  MARSHALL!

Result:   ARSHALL!

If I had !MARSHALL it does recognize it at the beginning.

Read only

Former Member
0 Likes
5,349

Please be certain that the character "!" is really hex 0021 and not something else.  Your description of the behavior seems to point to that character being something else, but is only displaying the "!".  You can see this in the debugger.  Also you might try simply replacing it with "!" by typing over the character in debugger.

Read only

RaymondGiuseppi
Active Contributor
0 Likes
5,349

You can get the list of allowed characters in the table RSALLOWEDCHAR.

(Table managed by the transaction RSKC reported by Krupa)

Thus, you can remove / replace illegal characters in the string or char fields with simple ABAP statements, but beware of the risk of generating duplicate keys!

Regards,

Raymond

Read only

Former Member
0 Likes
5,349

We too had a problem with strange characters in files coming from external sources( I believe it was an excel SS file that sometimes had some really odd characters)  We found just the FM to call to fix them.  Have a look at function module SCP_REPLACE_STRANGE_CHARS.  It does exactly what its name says.  Check out the Function Module Documentation for all the gory details. (It is one of the few SAP FMs that actually has some English documentation!)

Read only

Clemenss
Active Contributor
0 Likes
5,349

FORM replace_invalid_first_with_space changing cv_text type clike.

  STATICS:

    lv_invalid TYPE STRING.

  IF lv_invalid is initial.

    concatenate

    '!'

    cl_abap_char_utilities=>horizontal_tab "control characters visible as #

    cl_abap_char_utilities=>cr_lf

    cl_abap_char_utilities=>new_line

* add more invalid characters

    INTO lv_invalid.

  ENDIF

  IF strlen( cv_text ) > 0.

    IF lv_invalid CA cv_text(1).

      cv_text(1) = space.

    ENDIF.

  ENDIF.

ENDFORM.

Regards

Clemens

Read only

Former Member
0 Likes
5,350

The fix was created a FORM globally that replaces any hexidecimal characters 00-1F, which are not valid and also replacing the ! and # in the first field of the character.

FORM

*$*$ begin of 2nd part global - insert your code only below this line  *

FORM format_for_bw USING i_replace TYPE string

  CHANGING c_text_field TYPE c.

  DATA:  l_field_length TYPE i,

         l_index TYPE sy-index.

  FIELD-SYMBOLS <l_char> TYPE X.

*Hexadecimal character OO-1F are not valid in BW and therefore need to

*be replaced.

  l_field_length = STRLEN( c_text_field ).

  DO l_field_length TIMES.

*Retrieve the character's hex value

    l_index = sy-index - 1.

    assign c_text_field+l_index(1) to <l_char> casting.

*If the hex value is between 00 and 1F, replace with space

    IF <l_char> <= '1F'.

      c_text_field+l_index(1) = i_replace.

    ENDIF.

  ENDDO.

* Need to check the source field for any characters BW cannot handle

* ! as the first character, # in any position

  IF c_text_field(1) = '!'.

    SHIFT c_text_field LEFT DELETING LEADING '!'.

  ELSEIF c_text_field(1) = '#'.

    SHIFT c_text_field LEFT DELETING LEADING '#'.

  ENDIF.

ENDFORM.

Next for each text field that would have the potental of invalid characters apply the code below, which calls the FORM for cleanup of the characters. 

RESULT = SOURCE_FIELDS-LASTNAME.

    PERFORM format_for_bw USING SPACE

      CHANGING RESULT.