Skip to end of metadata
Go to start of metadata

Hi friends,

This wiki will serve as a basic guide to freshers who wants a knowledge on String processing. In this we will discuss each operations in details with basic examples so that it makes easy for the readers.

Introduction

Strings are the important part in our day to day programs. We will not be using the string which we have directly. We will be having scenarios where we need to process the available string and then display the strings according to our scenarios or requirements which we have.   

There are few basic and commonly used string functions which we will be using commonly which is discussed in detail below

String Processing

Concatenate 

If we want two or more small strings to be combined into a single string we have a function called as CONCATENATE. Concatenate function allows us to concatenate two or more strings in to a single string.

Syntax :

CONCATENATE dobj1 dobj2 ...N INTO result [stage:SEPARATED BY sep] [stage:RESPECTING BLANKS].

Code :

DATA: output TYPE string.
CONCATENATE 'HELLO'
            'WORLD'
            'GOODMORNING'
            INTO output.

WRITE: output.

Outputs will be

HELLOWORLDGOODMORNING

This looks more congested so in order to leave space between two words or to add any character/string between two words there is an option. In order to achieve that a few code ‘SEPERATED BY (Character/String for separation)’ must be added to the above code as shown below,

Code :

DATA: output TYPE string.
CONCATENATE 'HELLO'
            'WROLD'
            'GOODMORNING'
            INTO output
            SEPARATED BY SPACE.

WRITE: output.

Here I have used space as a separation so the output will look like

HELLO WORLD GOODMORNING

Sometimes there may be a situation where we need to give irregular spacing between words so that we can add space within the source text itself. But those spaces will not have effect in normal CONCATENATE function a small piece of code ‘RESPECTING BLANKS’ should be added along with it to make the spaces also visible as shown below,

Code:

DATA: output TYPE string.
CONCATENATE 'HELLO '
            'WROLD  '
            'GOODMORNING'
            INTO output
            RESPECTING BLANKS.

WRITE: output.

So now the output will look like

HELLO WROLD  GOODMORNING

Find

When we have a huge paragraph and we want to find a particular word in that paragraph then you go for this function ‘FIND’.

Syntax:

FIND [FIRST OCCURRENCE |stage: ALL OCCURRENCES OF]

         pattern IN [stage:section_of] dobj [RESPECTING | stage:IGNORING CASE]

[stage:MATCH COUNT  cnt]

[stage:MATCH OFFSET off]

[stage:MATCH LENGTH len]

[stage:SUBMATCHES s1 s2 ...].Generally we check the sy-subrc field to check whether the ‘FIND’ function is successful or not.

Code

DATA: output TYPE string.

FIND FIRST OCCURRENCE OF 'WORLD' in output. 

WRITE: sy-subrc.

Here now when we check the sy-subrc value it will be 0. We may have scenario where we would have the word WORLD type as small letters in a sentence or paragraph so in such a case we will go for another addition in the code. We need to add ‘IGNORING CASE’ so that cases will not be considered when FIND operation is performed.

Code

DATA: output TYPE string.

FIND FIRST OCCURRENCE OF 'WoRLD' in output IGNORING CASE. 

WRITE: sy-subrc.

Here if we don’t use the option ignoring case then the sy-subrc value will not be equal to 0. Here we used the option IGNORING CASES, so the FIND function will be satisfied and the sy-subrc value will be equal to 0.

We may also get a scenario where we need to find all occurrences of a single word for that we go for FIND ALL OCCURRENCES and in that we can find how many times the word has been repeated in the paragraph with use of MATCH COUNT cnt option. If we need to find the position where the word occurs in the sentence we go for MATCH OFFSET off option. If we need to find the length of the word which we are searching we can use an option MATCH LENGTH len.

Code

DATA: output TYPE string.
CONCATENATE 'HELLO '
            'WORLD  '
            'GOODMORNING '
            'WORLD'
            INTO output
            RESPECTING BLANKS.

DATA: cnt TYPE i,
      off TYPE i,
      len TYPE i.

FIND ALL OCCURRENCES OF 'WoRLD' IN output IGNORING CASE 

MATCH COUNT  cnt

MATCH OFFSET off

MATCH LENGTH len.

WRITE: / sy-subrc,
       / cnt,
       / off,
       / len.

The output will be 2 for CNT because the word ‘WORLD’ is repeated twice and the OFF will be having the value 25 the starting position of the word ‘WORLD’ (Here OFF is an variable so the position of the first occurrence is over written by the position of the second occurrence) and LEN will be 5 which is the length of the word for search.   

For sub matches we need to have knowledge about regular expressions. Please refer to the link given at references to get a basic knowledge about regular expressions.

Replace

There may be cases where we need not only find out the string, we need to replace the searched string with a new string. So we use REPLACE option to replace the searched string.

REPLACE [FIRST OCCURRENCE |stage: ALL OCCURRENCES OF]

                 pattern IN [stage:section_of] dobj WITH new [RESPECTING | stage:IGNORING CASE]        [stage:REPLACEMENT COUNT rcnt]

[stage:REPLACEMENT OFFSET roff]

[stage:REPLACEMENT LENGTH rlen].

This functions same as the FIND statement. But in the find we will be just searching for the required string but here we will also specify a replacement for the searched string.

Here also generally we check the sy-subrc field to check whether the ‘REPLACE’ function is successful or not.

Code

DATA: output TYPE string.

REPLACE FIRST OCCURRENCE OF 'WORLD' in output WITH 'FRIENDS'.  

WRITE: sy-subrc.

Here now when we check the sy-subrc value it will be 0. We may have scenario where we would have the word WORLD type as small letters in a sentence or paragraph so in such a case we will go for another addition in the code. We need to add ‘IGNORING CASE’ so that cases will not be considered when FIND and REPLACE operations is performed.

Code

DATA: output TYPE string.

REPLACE FIRST OCCURRENCE OF 'WoRLD' in output WITH 'FRIENDS' IGNORING CASE. 

WRITE: sy-subrc.

Here if we don’t use the option ignoring case then the sy-subrc value will not be equal to 0. Here we used the option IGNORING CASES, so the FIND function will be satisfied and the sy-subrc value will be equal to 0.

We may also get a scenario where we need to find all occurrences of a single word for that we go for FIND ALL OCCURRENCES and in that we can find how many times the word has been repeated in the paragraph with use of REPLACEMENT COUNT cnt option. If we need to find the position where the word occurs in the sentence we go for REPLACEMENT OFFSET off option. If we need to find the length of the word which we are searching we can use an option REPLACEMENT LENGTH len.

Code

DATA: output TYPE string.

DATA: cnt TYPE i,
      off TYPE i,
      len TYPE i.

REPLACE ALL OCCURRENCES OF 'WoRLD' IN output WITH 'FRIENDS'

REPLACEMENT  COUNT cnt

REPLACEMENT OFFSET off

REPLACEMENT LENGTH len.

WRITE: / sy-subrc,
       / cnt,
       / off,
       / len.

The output will be 2 for CNT because the word ‘WORLD’ is repeated twice and the OFF will be having the value 25 the starting position of the word ‘WORLD’ (Here OFF is an variable so the position of the first occurrence is over written by the position of the second occurrence) and LEN will be 5 which is the length of the word for replacement.   

For sub matches we need to have knowledge about regular expressions. Please refer to the link given at references to get a basic knowledge about regular expressions.

Shift

If we want to shift the string with respect to places or direction and also if we want to delete the characters and shift the string  we can go for ‘SHIFT’ function.

Syntax :

SHIFT dobj [ [stage:places]  [stage:direction] | deleting ] .

We may have a scenario where we need to shift the string right or left by certain places then we can use the option places and direction available in this.

Code :

DATA: output TYPE string.

output = 'HELLO WORLD GOODMORNING WORLD'.

SHIFT output LEFT BY 2 PLACES.

Suppose there is a case where we don’t know how many places we need to shift the string so in such case we can specify the sub string till which the string has to be shifted.

Code :

DATA: output TYPE string.

output = 'HELLO WORLD GOOD MORNING'.

SHIFT output LEFT UP TO 'WORLD'.

WRITE : output.

Here the output will be WORLD GOOD MORNING. The string is shifted left till occurrence of the word ‘WORLD’ and the remaining string is displayed as shown.

There may be cases where we need to delete either the leading or trailing text in a string and also shift the string according to the deleted text we can go for an option called LEFT DELETING LEADING and RIGHT DELETING TRAILING.

To delete the leading phrase of a text we can go for LEFT DELETING LEADING.

Code:

DATA: output TYPE string.

output = 'HELLO WORLD GOODMORNING WORLD'.

SHIFT output LEFT DELETING LEADING 'HELLO'.

WRITE : output.

The output will be as WORLD GOODMORNING WORLD. The word hello at the leading of the text is deleted and the remaining string is moved to the left position.

To delete the trailing phrase of a text we can go for RIGHT DELETING TRAILING.

Code :

DATA: output TYPE string.
output = 'HELLO WORLD GOODMORNING WORLD'.
SHIFT output RIGHT DELETING TRAILING 'WORLD'.
WRITE : output.

The output will be as ‘     HELLO WORLD GOODMORNING’. There will be few spaces added at the beginning of the string. These spaces are equal to the size of the word deleted at the end.

Split

We will not be using the given string as such we will be facing a situation where we need to split the string given based on some pattern and perform few operations, in such a case we will go for string function called SPLIT.

Syntax:

SPLIT dobj AT sep INTO  result1 result2 ...N | TABLE result_tab.

We can split the string based on any pattern and can store it in an internal table which can be processed later.

Code :

DATA:  output    TYPE string,

     Work_area TYPE string.

data : int_tab  TYPE TABLE OF string.

output = 'HELLO WORLD GOODMORNING WORLD'.
split output at space INTO TABLE itab.

loop at itab INTO wtab.
  write : / wtab.
endloop.

The output will be as

HELLO

WORLD

GOODMORNING

WORLD

The string is split based on the space in the string and it is displayed in the output.

Condense

When processing a string we may have the string which needs to be processed many have more than one space in-between the words. We can make the space between the words even by using the function CONDENSE.

Syntax:

CONDENSE text [stage:NO-GAPS].

Code :

DATA: output TYPE string.
output = 'HELLO WORLD   GOODMORNING WORLD'.
CONDENSE output.
Write / output.

The output will be as HELLO WORLD GOODMORNING WORLD. The unequal spaces in-between the words of the string are made equal and it is displayed.

There may also be scenarios where we need to avoid all the gaps between the words in a string. In such a case we go for an addition to the above code [stage:NO-GAPS].

Code :

DATA: output TYPE string.
output = 'HELLO WORLD   GOODMORNING WORLD'.
CONDENSE output NO-GAPS.
Write / output.

Here all the spaces in-between the words of the string are deleted and the string is displayed as HELLOWORLDGOODMORNINGWORLD.

Translate

There may be usual scenario where we need to convert the string written in lowercase letters to uppercase letters and uppercase letters to lowercase letters or we need to translate the words in the string with the help of a patter. In such a case we can go for TRANSLATE function.

Syntax:

TRANSLATE text TO UPPER | LOWER CASE | USING pattern.

Code :

DATA: output TYPE string.
output = 'HELLO WORLD GOODMORNING WORLD'.
TRANSLATE output to LOWER CASE.
Write / output.

The output will be in lowercase letters. We can also translate the text with a pattern.

Code :

DATA: output TYPE string.
output = 'HELLO WORLD GOODMORNING WORLD'.

TRANSLATE output using 'HEWGMN'.
Write / output.

Here the output will be EELLO GORLD GOODNORNING GORLD. The pattern works like this H->E i.e. the letters H in the output string are converted to E, W->G the letters W in the output string are converted to G and M->N the letters M in the output string are converted to N.

These are the few important operations which we can perform. I think it will clear all the doubts regarding string processing.

Thanks,

Sri Hari

  • No labels

2 Comments

  1. Former Member

    Hi,

    we can also perform  Finding Character Strings

          To search a character field for a particular pattern,use the SEARCH statement as follows:

          SEARCH <c> FOR <str> <options>.

       <option> in the SEARCH FOR statement can be any of the following:

     ABBREVIATED

                                   Searches the field <c> for a word containing the string in <str>. The characters can be separated by other characters. The first letter of the word  and the string <str> must be the same.

     STARTING AT <n1>

                                   Searches the field <c> for <str> starting at  position <n1>. The result SY-FDPOS refers to the offset relative to <n1> and not to the start of  the field.

       ENDING AT <n2>

                                   Searches the field <c> for <str> up to position <n2>.

         AND MARK

                                   If the search string is found, all the characters in the search string (and all the characters in between when using ABBREVIATED) are converted to upper case.

  2. Unknown User (kbmfyv0)

    Very useful tutorial.