In this high tech world of RESTful ABAP Programming, CDS, BOPF, BRF+, Fiori Elements etc, you might be smiling on reading this ancient String Operation topic at SAPYard. We completely understand your though process and even agree with it. But whatever technology SAP might come up with, clients always have some requirement, where we need fall back to basics. So, we are not ashamed to publish this article which will show the tip for those poor ABAP developers who have not yet seen the much hyped modern SAP System. More over, this is a real project scenario and the solution might be useful for someone in need in some corner of this SAP world.
Introduction:
As an ABAPer we all have worked with String in our Program and everyone is well aware of string operations in ABAP. Sometime we come across situation that we need to play with string in ABAP program.
Requirement:
To identify negative amount values in a long string and move negative sign from right to left of value. The negative sign can come in any place. It is not fixed.
For example, in below image negative amount values are at any place in long 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 can be done by below statement

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


We need OFFSET column from this table.
- We have to define two position pointers to hold high and low position value.
High value will point to 2nd row and Low value will point to 1st row OFFSET value in the table.

For example :

in this TEXT position and values are as below
Position | Value |
0 | # |
1 | 4 |
2 | 7 |
3 | 0 |
4 | . |
5 | 5 |
6 | 0 |
7 | – |
8 | # |
I need Low postion value to hold ‘1’ and High position value 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 have to check whether it has negative sign?
For this, we checked rightmost value is negative or not? If we found that the VALUE has negative sign at right, then we moved it to Left(as per user’s requirement)

- The final output is

Also Read: Utility Program to Auto Format the Texts into Meaningful Sentence
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.
This is my first article at SAPYard. I am planning to share all new stuff I learn in my project here. Your comments and feedback will surely help me share quality content. Please feel free to write the good, bad and ugly part of this tuturial.
cOMMENTS pLEASE!!!
Do join 6700+ SAP Technical Consultants in this Telegram SAP Technical Discuss Group. Ask, Answer and Learn is our Motto. You need to install Telegram App first in your mobile or desktop and then click the joining link.
Please SUBSCRIBE to SAPYard’s Youtube Channel for Free End to End SAP Video Course and Training.
Please follow our LinkedIn Page, LinkedIn Group, Facebook Page, Twitter and Instagram.
Do not forget to SUBSCRIBE to our YouTube Channel for Free Courses and Unconventional Interesting Videos.
Save our number +1-646-727-9273 and send us a Whatsapp message ‘LEARN’ to be part of our Learning Community.
Check some more Code Snippets at SAPYard
- Dynamic Where Condition usage in Database queries
- UNIX Command in SAP ABAP
- Send e-mail with subject line greater than 50 characters
- Pop Up Screen with Selection Option using FM POPUP_WITH_TABLE_DISPLAY
- String wrap
- Write Application Log
- Sample program to attach any file from application server to any Business Object
- Sample program to unlock the editor of a program.
- Why are developers so fond of ‘REUSE_ALV_GRID_DISPLAY’?
- DELETING rows of the internal table within the LOOP. Is it a Taboo? A big NO NO?
- Creating Dynamic Internal Table
- Selection Screen in SAP
- How to update custom field of PRPS table
- Get Latitude and Longitude of any place using Google Map API in SAP
- GPS like tool in SAP using Google Map API
- Know Who is doing What in your SAP System
- OOPs Report Using Splitter and ALV Tree Combination
- ALV with an Editable Row
- Extensive Tips and Tricks for Interactive SAP ALV
- How to Get Accurate Pricing in SD for Customer and Material?
- T-Guard: Transport Dependency Validation Tool
- Utility Program to Auto Format the Texts into Meaningful Sentence
- ABAP for SAP HANA. Part XX. ALV Report On SAP HANA – Opportunities And Challenges
- Selective Handling of the Buttons in ALV Grid Toolbar
- Working with Shell Scripts in SAP ABAP Environment
- CDS Part 7. Basic Expressions & Operations Available for CDS View – II
- Sudoku Solver and Generator Tool Using Vanilla SAP ABAP
- Calculator in SAP using New ABAP Syntax
- SO10 Enhancement to Add Texts to the Transport Request Automatically
- How to pass Reversal Date & Reason to BAPI_ACC_DOCUMENT_POST?
- ISU – Bankruptcy Overview and Write-Off Process using BAPI_CTRACDOCUMENT_WRITEOFF
- How to Create or Update Variant for a Report from another Program?
- A to Z of OLE Excel in ABAP 7.4
- SORTing Algorithm – Interview Question
- SOLMAN – Mail Forms Custom Enhancement Guide
- SORTing Algorithm – Performance Comparision
- Dynamic Code Processor
- Useful Trick to Search “Text” in Smartforms/Text modules
- Dynamically Download Data From Any SAP Table in ABAP-740 – Part 1
- How to SPLIT Data in FOR LOOP Using Modern ABAP Syntax?
- Dynamic Table Download in ABAP 751 – Part 2
- Consuming JSON based REST APIs in ABAP
- CDS Part 21 – CDS View Finder Tool
- ABAP String Operation – Identify & Manipulate Negative Amount in Long String with Separators