String Functions With Examples
String Functions With Examples
String Functions With Examples
1. CONCATENATE : - Concatenate is the string function used to combine different strings into one string. concatenate <c1> .. <cn> into <str> [separated by 's1']. DATA: CNTR(10) VALUE 'USA', STATE(10) VALUE 'CA', CITY(10) VALUE 'LA'. DATA STR(30). CONCATENATE CNTR STATE CITY INTO STR. WRITE STR. ****Result:-USACALA ****************************************** DATA: CNTR(10) VALUE 'USA', STATE(10) VALUE 'CA', CITY(10) VALUE 'LA'. DATA STR(30). CONCATENATE CNTR STATE CITY INTO STR SEPARATED BY ':'. WRITE STR. ****RESULT--USA:CA:LA =============================================================== 2. SPLIT :This string function is used to divide one string data into different strings. Syntax : Split <str> at <del>into <c1> .. <cn>. *EXAMPLEFORSPLIT DATA : A TYPE STRING VALUE 'INDIA@IS$GREAT', B TYPE STRING. SPLIT A AT '@' INTO A B. WRITE : A,/ B
http://sapbinavigations.blogspot.in/(PratapLavu)
Page 1
4. SHIFT:This string function is used to move the data of the string in required direction, and also used to delete unwanted data from the specified direction. This string function has 3 Syntaxes. 1st Syntax SHIFT <str> [by <n> places] [mode]. where n is number of characters. mode is : left / right / circular. DATA : STR(20) VALUE 'INDIA IS GREAT'. SHIFT STR BY 5 PLACES. " IS GREAT" WRITE STR. DATA : STR(20) VALUE 'INDIA IS GREAT'. SHIFT STR BY 5 PLACES RIGHT. WRITE STR. " INDIA IS GREAT " DATA : STR(20) VALUE 'INDIA IS GREAT'. SHIFT STR BY 5 PLACES CIRCULAR. WRITE STR. " IS GREAT INDIA" Syntax #2. shift <str> left deleting leading <s1>. data str(20) VALUE '### 00000008373'. SHIFT STR LEFT DELETING LEADING '0 #'. WRITE STR. http://sapbinavigations.blogspot.in/(PratapLavu) Page 2
5. CONDENSE :Using this function multiple spaces are converted into single space and deletes all other space from both right and left. 1)*Exampleforcondense data str(30) value 'SAP *(*&^%*&^^ INDIA'. REPLACE ALL OCCURRENCES OF '*(*&^%*&^^ ' IN STR WITH ''. CONDENSESTR. WRITE STR COLOR 7. ===================== 2)*Exampleforcondense data str(30) value 'SAP *(*&^%*&^^ INDIA'. REPLACE ALL OCCURRENCES OF '*(*&^%*&^^ ' IN STR WITH ''. *CONDENSESTR. CONDENSE STR NO-GAPS. WRITE STR COLOR 7.
6. REPLACE: Using this string function we can replace unwanted data with required characters. This string function has 2 syntaxes: i) REPLACE <S1> WITH <S2> INTO <STR> . here only first occurrence of <s1> will be replaced with <s2> ii) REPLACE ALL OCCURRENCES OF <S1> IN <STR> WITH <S2>. Example: data name(20) value 'nil kumr'. *replace '' with 'A' into name. replace all occurrences of '' in name with 'A'. write name. http://sapbinavigations.blogspot.in/(PratapLavu) Page 3
8. SEARCH:Is another string function is used to find for the data in a string. if data is found then returns "SY-SUBRC" ( predefined system variable) value as "0" else non zero. Example code. DATA STR(50) VALUE 'India is great'. search str for 'India'. if sy-subrc = 0. write 'search is successful'. else. write 'search failure'. endif.
http://sapbinavigations.blogspot.in/(PratapLavu)
Page 4
10. STRLEN:Is the string function is used to find data length of string. data str(20) value 'india is great'. data len type i. len = strlen( str ). write : str , 'length is ', len.
YYYYMMDD ( SAP FORMAT) MMDDYYYY ( USA FORMAT ) DATA : STR(8) VALUE '01272012'. DATA : DD(2), MM(2), YY(4). DD = STR+2(2). MM = STR+0(2). YY = STR+4. CONCATENATE YY MM DD INTO STR. WRITE STR.
DATA : STR(10) VALUE '01/27/2012'. DATA : DD(2), MM(2), YY(4). SPLIT STR AT '/' INTO MM DD YY. CONCATENATE YY MM DD INTO STR. WRITE STR. DATA : STR(10) VALUE '1/27/2012'. DATA : DD(2), MM(2), YY(4). http://sapbinavigations.blogspot.in/(PratapLavu) Page 5
http://sapbinavigations.blogspot.in/(PratapLavu)
Page 6