ABAP - String operations

String Operations:

String: It is a collection of characters. It is an elementary data type of variable length. String operations are applicable to the data objects having C(Character), N(Numeric), D(Date), T(Time) and string data types. 

String operations are not applicable to numeric datatypes: I(Integer) P (Packed number) F (Floating point)

String Operations:

CONCATENATE: 

DATAlv_input1(10TYPE VALUE 'Welcome',
      lv_input2(10TYPE VALUE 'To',
      lv_input3(10TYPE VALUE 'SAP-ABAP',
      lv_output     TYPE string.

CONCATENATE lv_input1 lv_input2 lv_input3 INTO lv_output SEPARATED BY ' '.
WRITE'The result is'lv_output.

SPLIT: Purpose of the split is to separate the string at recognizable separator.

DATAlv_input1(10TYPE VALUE 'Welcome',
      lv_input2(10TYPE VALUE 'To',
      lv_input3(10TYPE VALUE 'SAP-ABAP',
      lv_output     TYPE string.

DATAlv_result1(10TYPE C,
      lv_result2(10TYPE C,
      lv_result3(10TYPE C.

CONCATENATE lv_input1 lv_input2 lv_input3 INTO lv_output SEPARATED BY SPACE.
WRITE'The result is'lv_output.

SPLIT lv_output at SPACE INTO lv_result1 lv_result2 lv_result3.
WRITE'Result after split'/ lv_result1/ lv_result2/ lv_result3.

CONDENSE: Used to remove leading and trailing spaces and convert sequence of spaces into single space. 

DATAlv_input TYPE string VALUE ' Welcome    To    SAP-ABAP '.
WRITE'Before Condense'lv_input.
CONDENSE lv_input.  "removes gaps but leave one gap
WRITE'After Condense'lv_input.
CONDENSE lv_input NO-GAPS"no gaps at all
WRITE'After Condense No-Gaps'lv_input.

STRLEN: Used to find the length of the string.

DATAlv_input TYPE string VALUE ' Welcome    To    SAP-ABAP '.
DATAlv_length(2TYPE N.
lv_length STRLENlv_input ).
WRITE/'Length of the string'lv_length.
WRITE'Before Condense'lv_input.

CONDENSE lv_input.  "removes gaps but leave one gap
WRITE'After Condense'lv_input.
lv_length STRLENlv_input ).
WRITE/'Length of the string'lv_length.

CONDENSE lv_input NO-GAPS"no gaps at all
WRITE'After Condense No-Gaps'lv_input.
lv_length STRLENlv_input ).
WRITE/'Length of the string'lv_length.

FIND: Find a particular pattern in the string. 

DATAlv_input(50TYPE VALUE 'Greatest adventure ever'.
*FIND 'Greatest' IN lv_input.
FIND 'greatest' IN lv_input IGNORING CASE"Ignore upper case or lower case
*SY-SUBRC:
IF sy-subrc 0.
  WRITE'Successful'sy-subrc.
ELSEIF sy-subrc 1.
  WRITE'Not successful'sy-subrc.
ENDIF.

TRANSLATE: To convert string into upper case and lower case.

DATAlv_input(50)  TYPE VALUE 'Weather is Nice',
      lv_input1(50TYPE VALUE 'weather is nice',
      lv_rule(10)   TYPE VALUE 'wWiInN'"Own rule can be declared to custom change the case of the alphabet. 

TRANSLATE lv_input1 USING lv_rule.
WRITE'Output using our own pattern:',lv_input1.

TRANSLATE lv_input TO LOWER CASE.
WRITElv_input.

TRANSLATE lv_input1 TO UPPER CASE.
WRITElv_input1.

SHIFT: To shift contents of the string.

DATAlv_input1(10TYPE VALUE '0123456789',
*      C is character but it can accept alphabets as well as numbers because it is alphanumeric
      lv_input2(10TYPE VALUE '0123456789',
      lv_input3(10TYPE VALUE '0123456789'.
SHIFT lv_input1 BY PLACES"By default sustem shifts the string to the left
WRITE/'Left:'lv_input1.
SHIFT lv_input2 BY PLACES RIGHT.
CONDENSE lv_input2"By default sustem shifts the string to the left
WRITE/'Right:'lv_input2.
SHIFT lv_input3 BY PLACES CIRCULAR"By default sustem shifts the string to the left
WRITE/'Circular:'lv_input3.

Shift: Deleting trailing and leading zeroes. Note: below code was not working when I tried to change 9 to 10 in datatype. Check it in future. 

DATAlv_result3(9value '000000005'.
DATAlv_result4(9value '500000000'.
SHIFT lv_result3 LEFT DELETING LEADING '0'.
WRITE/'Result after leading zero deletion'lv_result3.
SHIFT lv_result4 RIGHT DELETING TRAILING '0'.
CONDENSE lv_result4.
WRITE/'Result after trailing zero deletion'lv_result4.

SUBSTRING: Part of a string

Target string = source variable[+][Starting position of the substring][Length of the substring]

Note: There should be not space between source variable and plus sign and there should be space between target string and equal to sign

DATAlv_value(50TYPE VALUE '01-151-2657652345',
      lv_country(2TYPE C,
      lv_city(3TYPE C,
      lv_phonenumber(10TYPE C.
lv_country lv_value+0(2).
WRITE'The country code is',lv_country.
lv_city lv_value+3(3).
WRITE'The city code is',lv_city.
lv_phonenumber lv_value+7(10).
WRITE'The phone number is',lv_phonenumber.

String Comparison Operators:

CO: Contains Only

DATAlv_string1(20TYPE VALUE 'webz',
      lv_string2(30TYPE VALUE 'Web application model'.

* String comparison operator CO (Contains only - E
* V1 CO V2: V1 contains characters that are in V2. If V1 has any character that is not in V2, False is returned.
IF lv_string1 CO lv_string2.
  WRITE'True'sy-fdpos.  "True will return length of character that match.
ELSE.
  WRITE:'False'sy-fdpos"Gives the location or offset of the position where the string does not match. In this case at 6.
ENDIF.

Case sensitvity:

DATAlv_string1(20TYPE VALUE 'Web Application Development',
      lv_string2(30TYPE VALUE 'Web application development'.

* String comparison operator CO (Contains only - E
* V1 CO V2: V1 contains characters that are in V2. If V1 has any character that is not in V2, False is returned.
* Even if character is not at same location, as long as characters in both strings match, true is returned. 
IF lv_string1 CO lv_string2.
  WRITE'True'sy-fdpos.  "True will return length of character that match.
ELSE.
  WRITE:'False'sy-fdpos"gives the location or offset of the position where the string does not match. In this case at 6.
ENDIF.

CN: Contains Not Only

DATAlv_string1(20TYPE VALUE 'Webz',
      lv_string2(30TYPE VALUE 'Web application development'.

* String comparison operator CN (Contains not only)
* V1 CN V2: V1 contains characters that are not in V2. If V1 has any character that is in V2, False is returned.
* Even if character is not at same location, as long as characters in both strings match, true is returned. 
IF lv_string1 CN lv_string2.
  WRITE'True'sy-fdpos.  "True will return length of character that match.
ELSE.
  WRITE:'False'sy-fdpos"gives the location or offset of the position where the string does not match. In this case at 6.
ENDIF.

CN: Contains Any

* Example that password should consist of at least one number
DATAlv_string1(10TYPE VALUE 'test@12345',
      lv_string2(10TYPE VALUE '0123456789',
      lv_string3(10TYPE VALUE 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.

* String comparison operator CA (Contains Any)
* V1 CA V2: True = V1 contains at least one characters that are in V2. 
* False: V1 does not contain any character that are in V2
 
IF lv_string1 CA lv_string2.
  WRITE'True'sy-fdpos.  "Offset of first character of V1 that is in V2
ELSE.
  WRITE:'False'sy-fdpos"Length V1
ENDIF.

IF lv_string1 CA lv_string3.
  WRITE'True'sy-fdpos.  "Offset of first character of V1 that is in V2
ELSE.
  WRITE:'False'sy-fdpos"Length V1
ENDIF.

NA: Contains Not Any

DATAlv_string1(10TYPE VALUE 'test@12345',
      lv_string2(10TYPE VALUE '0123456789',
      lv_string3(10TYPE VALUE 'test@abcd',
      lv_string4(10TYPE VALUE 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.

* String comparison operator NA (Contains Not Any). String comparison is case sensitive. 
* V1 NA V2: True = V1 does not contain any character that are in V2
* False: V1 contains at least one characters that are in V2.

* Scenerio: V1 has some characters that are in V2, so Else statement will be called.
IF lv_string1 NA lv_string2.
  WRITE/ 'True'sy-fdpos.  "Length V1
ELSE.
  WRITE:/ 'False'sy-fdpos"Offset of first character of V1 that is in V2
ENDIF.

* Scenerio: V2 has no characters that are in V3, so Else statement will be called.
IF lv_string2 NA lv_string3.
  WRITE/ 'True'sy-fdpos.  "Length V1
ELSE.
  WRITE:/ 'False'sy-fdpos"Offset of first character of V1 that is in V2
ENDIF.

CS: Contains String

DATAlv_string1(20TYPE VALUE 'Web application development',
      lv_string2(30TYPE VALUE 'Web'.

* V1 CS V2: True = V1 contains string from V2
* False: V1 does not contains string from  V2. CS is not case sensitive.

IF lv_string1 CS lv_string2.
  WRITE/ 'True'sy-fdpos.  "Offset of first character of V1 that is in V2
ELSE.
  WRITE:/ 'False'sy-fdpos"Length V1
ENDIF.

CO: Contains Only

DATAlv_string1(20TYPE VALUE 'webdev',
      lv_string2(30TYPE VALUE 'vedbew'.

* V1 CO V2: True = V1 contains characters that are in V2
* False: V1 does not contains characters from  V2.
* CO works on individual characters. CO is case sensitive. 

IF lv_string1 CO lv_string2.
  WRITE/ 'True'sy-fdpos"Length V1 
ELSE.
  WRITE:/ 'False'sy-fdpos"Offset of first character of V1 that is in V2
ENDIF.


https://youtu.be/gn-UWR43QtY?si=sb6uMe09IQ43hPrc


No comments:

Post a Comment