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:
DATA: lv_input1(10) TYPE c VALUE 'Welcome',
lv_input2(10) TYPE c VALUE 'To',
lv_input3(10) TYPE c 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.
DATA: lv_input1(10) TYPE c VALUE 'Welcome',
lv_input2(10) TYPE c VALUE 'To',
lv_input3(10) TYPE c VALUE 'SAP-ABAP',
lv_output TYPE string.
DATA: lv_result1(10) TYPE C,
lv_result2(10) TYPE C,
lv_result3(10) TYPE 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.
DATA: lv_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.
DATA: lv_input TYPE string VALUE ' Welcome To SAP-ABAP '.
DATA: lv_length(2) TYPE N.
lv_length = STRLEN( lv_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 = STRLEN( lv_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 = STRLEN( lv_input ).
WRITE: /'Length of the string', lv_length.
FIND: Find a particular pattern in the string.
DATA: lv_input(50) TYPE c 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.
DATA: lv_input(50) TYPE c VALUE 'Weather is Nice',lv_input1(50) TYPE c VALUE 'weather is nice',
lv_rule(10) TYPE c 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.
WRITE: lv_input.
TRANSLATE lv_input1 TO UPPER CASE.
WRITE: lv_input1.
SHIFT: To shift contents of the string.
DATA: lv_input1(10) TYPE c VALUE '0123456789',
* C is character but it can accept alphabets as well as numbers because it is alphanumeric
lv_input2(10) TYPE c VALUE '0123456789',
lv_input3(10) TYPE c VALUE '0123456789'.
SHIFT lv_input1 BY 5 PLACES. "By default sustem shifts the string to the left
WRITE: /'Left:', lv_input1.
SHIFT lv_input2 BY 5 PLACES RIGHT.
CONDENSE lv_input2. "By default sustem shifts the string to the left
WRITE: /'Right:', lv_input2.
SHIFT lv_input3 BY 4 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.
DATA: lv_result3(9) value '000000005'.
DATA: lv_result4(9) value '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.
DATA: lv_value(50) TYPE C VALUE '01-151-2657652345',
lv_country(2) TYPE C,
lv_city(3) TYPE C,
lv_phonenumber(10) TYPE 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
DATA: lv_string1(20) TYPE c VALUE 'webz',
lv_string2(30) TYPE c 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:
DATA: lv_string1(20) TYPE c VALUE 'Web Application Development',
lv_string2(30) TYPE c 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
DATA: lv_string1(20) TYPE c VALUE 'Webz',
lv_string2(30) TYPE c 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
DATA: lv_string1(10) TYPE c VALUE 'test@12345',
lv_string2(10) TYPE c VALUE '0123456789',
lv_string3(10) TYPE C 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
DATA: lv_string1(10) TYPE c VALUE 'test@12345',
lv_string2(10) TYPE c VALUE '0123456789',
lv_string3(10) TYPE c VALUE 'test@abcd',
lv_string4(10) TYPE C 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
DATA: lv_string1(20) TYPE c VALUE 'Web application development',
lv_string2(30) TYPE c 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
DATA: lv_string1(20) TYPE c VALUE 'webdev',
lv_string2(30) TYPE c 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