ABAP String Operation – Identify & Manipulate Negative Amount in Long String with Separators

ABAP String Operation – Identify & Manipulate Negative Amount in Long String with Separators

In this super advanced universe of Tranquil ABAP Programming, Cds, BOPF, BRF+, Fiori Components and so forth, you may be blessing perusing this antiquated String Activity theme. We totally comprehend your however cycle and even concur with it. In any case, anything innovation SAP could think of, clients generally have some prerequisite, where we really want fall simple. Thus, we are not afraid to distribute this article which will show the tip for those poor ABAP designers who have not yet seen the much advertised present day SAP Framework. More finished, this is a genuine undertaking situation and the arrangement may be helpful for somebody deprived in some side of this SAP world.

Introduction:

As an ABAPer we as a whole have worked with String in our Program and everybody is very much aware of string tasks in ABAP. At some point we run over circumstance that we really want to play with string in ABAP program.

Requirement:

To recognize negative sum values in a long string and move negative sign from right to left of significant worth. The negative sign can come in any spot. It isn’t fixed.

For instance, in underneath picture negative sum values are at any spot in lengthy string.

Logic:

  • Identify the separators.

Here separators are ‘#’. Sometimes it may be ‘;’ or ‘,’.

  • Identify total number of separators in a string and their position.

This should be possible by underneath explanation

RESULT_TAB will hold position of ‘#’ in the string.

For example :

We want OFFSET segment from this table.

  • We have to define two position pointers to hold high and low position value.

High worth will highlight second column and Low worth will highlight first line OFFSET esteem in the table.

For example :

in this TEXT position and values are as underneath

Position Value
0 #
1 4
2 7
3 0
4 .
5 5
6 0
7
8 #

I really want Low postion worth to stand firm on ‘1’ and High foothold worth to hold ‘8’

  • Calculate the difference between High and Low value position.

 That means the number is 7 digit long.

  • Read this 7 digit long value in one variable called VALUE.
  • Once we get the value, we can play with it like moving negative sign in front of the value.

First we need to check whether it has negative sign?

For this, we checked furthest right worth is negative or not? In the event that we found that the Worth has negative sign at right, we moved it to Left(as per client’s prerequisite)

  • The final output is

Complete Program:

REPORT ZSAT_TEST_STRING.

DATA :  SIGN        TYPE STRING VALUE '-',
        SEPARATOR   TYPE STRING VALUE '#',
        TEXT        TYPE STRING,
        TEXT1       TYPE STRING,
        VALUE       TYPE STRING,
        LV_STRLEN   TYPE I,
        LV_INDEX    TYPE I,
        LV_POS      TYPE I,
        LV_OFFSET_H TYPE I,
        LV_OFFSET_L TYPE I,
        LV_DIFF     TYPE I,
        LV_DIFF1    TYPE I,
        RESULT_TAB  TYPE MATCH_RESULT_TAB,
        WA_TAB      LIKE LINE OF RESULT_TAB,
        WA_TAB1     LIKE LINE OF RESULT_TAB,
        WA_TAB2     LIKE LINE OF RESULT_TAB.

***For test case string value is taken directly.

TEXT = '#470.50-##SA#20191130#40#42.57-#UAH###0034821037#20191219#20191231#20191231#100.75-'.

TEXT1 = TEXT.

CLEAR LV_STRLEN.
LV_STRLEN = STRLEN( TEXT ).

REFRESH RESULT_TAB[].

FIND ALL OCCURRENCES OF SEPARATOR IN TEXT RESULTS RESULT_TAB.

*FIND ALL OCCURRENCES OF CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB IN TEXT RESULTS RESULT_TAB.
IF NOT RESULT_TAB[] IS INITIAL .
  SORT RESULT_TAB BY OFFSET.
***Appending Last Line in Result_Tab in case last value is negative
  CLEAR WA_TAB2.
  READ TABLE RESULT_TAB INTO WA_TAB2 INDEX 1.
  WA_TAB2-OFFSET = LV_STRLEN.
  APPEND WA_TAB2 TO RESULT_TAB.

***Looping Result Tab
  LOOP AT RESULT_TAB ASSIGNING FIELD-SYMBOL(<FS_TAB>).
    IF SY-TABIX = 1.
      CONTINUE.
    ENDIF.

    CLEAR : LV_INDEX, LV_POS, LV_OFFSET_H, LV_OFFSET_L, VALUE, LV_DIFF, LV_DIFF1.

***To extract the VALUE between two separators.
    LV_INDEX = SY-TABIX.
    LV_POS = <FS_TAB>-OFFSET - 1.
    LV_OFFSET_H = <FS_TAB>-OFFSET.
    LV_INDEX = LV_INDEX - 1.
    CLEAR WA_TAB.
    READ TABLE RESULT_TAB INTO WA_TAB INDEX LV_INDEX.
    IF SY-SUBRC = 0.
      LV_OFFSET_L = WA_TAB-OFFSET.
    ENDIF.
    LV_OFFSET_L = LV_OFFSET_L + 1.
    LV_DIFF = LV_OFFSET_H - LV_OFFSET_L.

    VALUE = TEXT+LV_OFFSET_L(LV_DIFF).

***To move negative sign in front of amount value.
    LV_DIFF1 = LV_DIFF - 1.
    IF LV_DIFF1 GT 2.
      IF VALUE+LV_DIFF1(1) = SIGN.
        CALL FUNCTION 'CLOI_PUT_SIGN_IN_FRONT'
          CHANGING
            VALUE = VALUE.
        REPLACE TEXT+LV_OFFSET_L(LV_DIFF) IN TEXT WITH VALUE.
      ENDIF.
    ENDIF.
  ENDLOOP.
ENDIF.
WRITE : / 'Input String'.
WRITE : / TEXT1.
WRITE : / 'Output String'.
WRITE : / TEXT.

 

YOU MAY BE INTERESTED IN

ABAP Applications for the Cloud: Modernizing for the Future

Your Guide To Data Science Career 2024

ABAP Development Environment in the Cloud

X
WhatsApp WhatsApp us