String Functions in SAP ABAP ( New way of using string operations )
STRING FUNCTIONS in SAP ABAP:
sap has added some interesting new string functions from versions 7.2.
In few cases the new functions replace previous ABAP commands.
List of useful string functions:
1.CONCATENATE:
Purpose: it combines two or more separate strings into one.
Syntax
*<output string> = |{ string value }| & | | & |{ string2 value }|.
Example: REPORT yabap0006.
DATA(str1) = 'Welcome to SAP'. DATA(str2) = 'Team'.
DATA(result) = |{ str1 }| & | | & |{ str2 }|. " Concatenate with space
DATA(result) = |{ str1 }| & |{ str2 }|. " concatenate without space WRITE result.
Or we can use the new way concatenate as below: *&concatenate strings while displaying output
*write: / |{ string value } { string2 value }| " Concatenate with space
write: / |{ str1 } { str2 }|. "Concatenate with space write: / str1 && str2. "concatenate without space
Output:
1.welcome to sap team.
2.welcome to sapteam.
2.CONDENSE:
Purpose:
In the variable text, any leading and trailing blanks are removed and any other blanks that follow each other directly are replaced by exactly one blank or, if NO-GAPS is specified, are also removed completely.
Syntax
... condense( [val =] text [del = del] [from = from] [to = to] ) ...
Example :
REPORT yabap0006. DATA(str1) = 'Welcome to SAP'.
str1 = CONDENSE( VAL = str1 to = ''). " no gaps
str1 = CONDENSE( VAL = str1 ). " One gap of space in between words WRITE str1.
Output:
1.welcometosap.
2.welcome to sap.
3.TO LOWER/TO UPPER :
Purpose:
These functions return the character string from text after it has been converted according to the following case rules:
The function to_upper converts all letters in the character string to uppercase letters.
The function to_lower converts all letters in the character string to lowercase letters.
Syntax
1. ... to_upper( [val =] text ) ...
2. ... to_lower( [val =] text ) ...
Example:
REPORT yabap0006.
DATA(str1) = 'Welcome to SAP'.
DATA(upper) = to_upper( str1 ).
WRITE / upper. DATA(lower) = to_lower( str1 ). WRITE / lower.
Output:
1.WELCOME TO SAP.
2.welcome to sap.
4.SHIFT:
Purpose:
The shift functions shift the content of a character-like argument.
Syntax
1. ... shift_left( [val =] text [places = places]|[circular = places]|[sub = substring] ) ...
2. ... shift_right( [val =] text [places = places]|[circular = places]|[sub = substring] ) ...
Example:
REPORT yabap0006.
DATA(str1) = 'Welcome to SAP'.
DATA(result) = shift_left( val = str1 places = 3 ). "remove spaces from left
DATA(result) = shift_right( val = str1 places = 3 ). "remove spaces from rig ht
DATA(result) = shift_right( val = str1 circular = 3 ). "shift spaces circularly from right to left or left to right.
write result.
output:
1.come to SAP.
2.Welcome to.
3.SAP Welcome to.
5.REPLACE/REPLACE ALL:
Purpose:
This function replaces a subfield of text with the character string specified in new and returns the changed text.
1. ... replace( val = text [off = off] [Len = Len] with = new ) ...
2. ... replace( val = text {sub = substring}|{regex = regex} with = new [case = case] [occ = occ] ) ...
Example:
REPORT yabap0006.
DATA(result) = replace( val = 'SAPP' regex = 'P' WITH = 'A' occ = '0' ). " use occ = 0 to replace all WRITE result.
Output:
SAAA.
6. REVERSE:
Purpose:
This function returns a character string that reverses the content of text.
Syntax
... reverse( [val =] text ) ...
Example: REPORT yabap0006.
DATA(str1) = 'Welcome to SAP'. str1 = Reverse( str1 ).
WRITE str1.
Output: PAS ot emocleW.
7.REPEAT:
Purpose:
This function returns a character string that contains the content of text as many times as specified in occ. If text is an empty string or if occ contains the value 0, an empty string is returned.
Syntax
... repeat( val = text occ = occ ) ...
Example: REPORT yabap0006.
data(result) = repeat( val = `SAP` occ = 10 ).
WRITE / result.
Output:
SAPSAPSAPSAPSAPSAPSAPSAPSAPSAP
it is very useful to me sir ...thank you for posting this.
ReplyDelete