\`...\`) and have the data type `string`.
- Text field literals are enclosed in single quotes (`'...'`) and have the data type `c`.
- The literals can be (but should not according to the [programming guidelines on literals (F1 docu for standard ABAP)](https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abenliterals_guidl.htm)) used like constants of these types in [operand positions](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenoperand_position_glosry.htm). They should be only used for start values when declaring [named data objects](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abennamed_data_object_glosry.htm).
```abap
CLASS zcl_some_test_class DEFINITION PUBLIC FINAL CREATE PUBLIC.
PUBLIC SECTION.
INTERFACES if_oo_adt_classrun.
ENDCLASS.
CLASS zcl_some_test_class IMPLEMENTATION.
METHOD if_oo_adt_classrun~main.
out->write( `I am a text string literal` ). "text string literal of type string
out->write( 'I am a text field literal' ). "text field literal of type c
ENDMETHOD.
ENDCLASS.
```
- [Named](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abennamed_data_object_glosry.htm) character-like data types and objects can be declared like other types and objects using [`TYPES`](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abaptypes.htm), [`DATA`](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abapdata.htm) [`CONSTANTS`](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abapconstants.htm) and by referring to a character-like data type.
- In addition, character-like data objects can be declared inline with the operators `DATA` and [`FINAL`](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenfinal_inline.htm).
Syntax examples:
``` abap
"Type declarations using built-in types
TYPES: c_type TYPE c LENGTH 3, "Explicit length specification
str_type TYPE string.
"Data object declarations using built-in, local and DDIC types
DATA: flag TYPE c LENGTH 1, "Built-in type
str1 TYPE string, "Built-in type
char1 TYPE c_type, "Local type
str2 LIKE str1, "Deriving type from a local data object
str3 TYPE str_type, "Local type
tstmp TYPE timestampl, "DDIC type
char3 TYPE zdemo_abap_flsch-carrid. "Using the type of a DDIC table component
"You may also encounter declarations with type c and the length
"specified in parentheses. This is not recommended, to avoid confusion
"with the use of parentheses in dynamic programming.
DATA char(4) TYPE c.
"Just a TYPE c specification without length means LENGTH 1.
DATA char_len_one TYPE c.
"No type and length specification: TYPE c LENGTH 1 by default
DATA char_no_type_len.
```
## Assigning Values
- When you declare character-like data objects, you can specify start values directly with the `VALUE` addition, e.g. `DATA chars TYPE c LENGTH 3 VALUE 'abc'.`.
- You can do value assignments to data objects using the the [assignment operator](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenassignment_operator_glosry.htm "Glossary Entry") `=`.
- As mentioned above, you can declare character-like data objects inline using the operators `DATA` or `FINAL`.
- You can use the operators at many [write positions](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenwrite_position_glosry.htm "Glossary Entry").
- Unlike the `VALUE` addition of the declaration statements, inline declarations allow you to declare variables for the results of expressions or at other positions where character strings are returned.
- In the case below, a variable specified in parentheses preceded by `DATA` (or `FINAL`) on the left side of the assignment operator automatically derives a data type from the operand on the right. This helps to make your
programs leaner.
Syntax examples:
``` abap
"Data object declarations including default values with VALUE
"Note the chained statement: DATA followed by a colon, listing the data object declarations,
"separated by a comma.
DATA: flag TYPE c LENGTH 1 VALUE 'X',
str1 TYPE string VALUE `Hallo!`.
"Examples for type n
DATA zip_code TYPE n LENGTH 5 VALUE '12345'.
DATA isbn_number TYPE n LENGTH 13 VALUE '1234567890123'.
"Constant; content cannot be changed at runtime
CONSTANTS pi TYPE p LENGTH 8 DECIMALS 14 VALUE '3.14159265358979'.
"More data object declarations
DATA: char1 TYPE c LENGTH 5,
html TYPE string,
str2 LIKE html.
"Value assignments
char1 = 'ab123'.
html = `hallo
`. "Escaping backquotes in text string literals with another one str1 = `This is a backquote: ``.`. "If possible, avoid unnecessary type conversion; in principle, every "convertible type can be specified str2 = 'abc'. "Fixed length string assigned to data object of type string DATA str3 TYPE string VALUE 'X'. "type c length 1 DATA str4 TYPE string VALUE -1. "type i "Inline declarations DATA(char2) = 'abcd'. "Type c length 4 DATA(str5) = `efgh`. "You can use FINAL to create immutable variables. FINAL(final_string) = `zyx`. "Since char2 is of type c length 4 (the length is also derived), "characters are truncated in the following example assignment "Note: In newer ABAP releases, the following statement shows a syntax "warning that the value of the literal (intentionally specified "here like this) is not an admissable value for the target type. char2 = 'ijklmnopq'. "ijkl "Trailing blanks after assigning fixed length to variable length string DATA(char3) = 'ab '. DATA(str6) = `cdefgh`. str6 = char3. "'ab' (trailing blanks are not respected due to conversion rule) ``` - When assigning strings, not only data objects can be placed on the right side. Various expressions and strings can be concatenated using the [concatenation operator](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenconcatenation_operator_glosry.htm "Glossary Entry") `&&`. - Alternatively, you can concatenate strings using [string templates](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenstring_template_glosry.htm "Glossary Entry"), as described in the *Concatenating Strings* section. ``` abap str5 = str3 && ` ` && str4 && `!`. "X 1-! "Note the output for str4 that includes the conversion of type i to "string above demonstrating a possibly inadvertent specification "of an integer value for str4 that is of type string. ``` Note that there is also the [literal operator](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenliteral_operator_glosry.htm "Glossary Entry") `&` that joins [text string literals](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abentext_string_literal_glosry.htm "Glossary Entry"), however, with [significant differences](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenliteral_operator.htm) to `&&`. ## String Templates - Using string templates, you can construct strings very elegantly from literal text and - which is the primary use case - by including embedded ABAP [expressions](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenexpression_glosry.htm "Glossary Entry") within a pair of delimiters (`|...|`) if these expressions can be converted to `string`. - To embed expressions, you enclose them in curly brackets: `{ ... }`. - Among the expressions that can be specified in the curly brackets are data objects and [functional method calls](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenfunctional_method_call_glosry.htm) that have a [return value](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenreturn_value_glosry.htm). The expression result must be convertible to type `string`. > **💡 Note**Hallo\n
Ciao!
Salut.
|. DATA(html_b) = html_a. REPLACE ALL OCCURRENCES OF PCRE `()(.*?)(<\/p>)` IN html_a WITH `$1Hi$3`. *
Hallo *
Hi
Hi
"Regular expression: any character or a new line with zero or more repretitions REPLACE ALL OCCURRENCES OF PCRE `()(.|\n)*?(<\/p>)` IN html_b WITH `$1Hi$3`. *
Hi
Hi
Hi
``` ## More String Functions As also covered in the [Built-In Functions](24_Builtin_Functions.md), the following sections show more string functions available. ### Checking the Similarity of Strings - [`distance`](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abendistance_functions.htm) returns the Levenshtein distance between two strings, which reflects their similarity. - Unlike other string functions, the return value has the type `i`. - Optional addition `max`: Positive integer. The calculation of the Levenshtein distance will stop if the calculated value is greater than this integer. ```abap DATA(str_to_check) = `abap`. DATA(dist1) = distance( val1 = str_to_check val2 = `abap` ). "0 DATA(dist2) = distance( val1 = str_to_check val2 = `axbap` ). "1 DATA(dist3) = distance( val1 = str_to_check val2 = `yabyyapy` ). "4 DATA(dist4) = distance( val1 = str_to_check val2 = `zabapzzzzzzzzzzzz` max = 5 ). "5 "If the value of max is 0 or less, an exception is raised. TRY. DATA(dist5) = distance( val1 = str_to_check val2 = `#ab#ap#` max = 0 ). CATCH cx_sy_strg_par_val. ... ENDTRY. ``` ### Repeating Strings - [`repeat`](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenrepeat_functions.htm) returns a string that contains the content of a specified string for parameter `val` as many times as specified in the parameter `occ`. - An empty string is returned when `occ` has the value 0 or `val` is empty. ```abap DATA(repeat1) = repeat( val = `abap` occ = 5 ). "abapabapabapabapabap DATA(repeat2) = |#{ repeat( val = ` ` occ = 10 ) }#|. "# # DATA(repeat3) = COND #( WHEN repeat( val = `a` occ = 0 ) = `` THEN `Y` ELSE `Z` ). "Y (initial value returned) "If occ has a negative value, an exception is raised. TRY. DATA(repeat4) = repeat( val = `X` occ = -3 ). CATCH cx_sy_strg_par_val. ... ENDTRY. ``` ### Returning the Smallest/Biggest of a Set of Character-Like Arguments - [`cmin/cmax`](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abencmax_cmin_functions.htm) returns a string that contains the content of the smallest or biggest of a set of character-like arguments - 'Set' means at least two arguments and a maximum of nine argeuments are passed (`valn` operators) for comparison. - The comparison is made from left to right, and the first different character found determines the smaller or bigger argument. ```abap DATA(min) = cmin( val1 = `zzzzzzz` val2 = `zzazzzzzzzz` "smallest argument val3 = `zzzzabc` ). DATA(max) = cmax( val1 = `abcdef` "biggest argument val2 = `aaghij` val3 = `aaaaklmn` val4 = `aaaaaaopqrs` val5 = `aaaaaaaaaatuvwxy` val6 = `aaaaaaaaaaaaaz` ). ``` ### Escaping Special Characters - [`escape`](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenescape_functions.htm) returns a string that is provided for the `val` parameter by escaping special characters according to the specification in the `format` parameter. - Suitable values for the `format` parameter (which expects a data object of type `i`) are available in the `CL_ABAP_FORMAT` class (the constants starting with `E_`). - Special rules apply to different contexts, such as URLS and JSON. Also note the prevention of Cross Site Scripting (XSS) attacks on web applications. For more information, refer to the [documentation](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenescape_functions.htm). ```abap "Context: URLs DATA(esc1) = escape( val = '...test: 5@8...' format = cl_abap_format=>e_url_full ). "...test%3A%205%408... "Context: JSON DATA(esc2) = escape( val = 'some "test" json \ with backslash and double quotes' format = cl_abap_format=>e_json_string ). "some \"test\" json \\ with backslash and double quotes "Context: String templates DATA(esc3) = escape( val = 'Special characters in string templates: |, \, {, }' format = cl_abap_format=>e_string_tpl ). "Special characters in string templates: \|, \\, \{, \} "Invalid value for the format parameter TRY. DATA(esc4) = escape( val = 'This will raise an exception due to an invalid format value.' format = 123 ). CATCH cx_sy_strg_par_val. ENDTRY. ``` ## Excursions ### Comparison Operators for Character-Like Data Types in a Nutshell The comparison operators have already been covered in different sections above. This is a summary of the operators.| Comparison Operator | Details | Example |
|---|---|---|
CA |
Contains any To determine whether any character of a given character set is contained in a string. Note: The search is case-sensitive. sy-fdpos contains the offset of the first character found, while 0 stands for the very first position. If nothing is found, sy-fdpos contains the length of the string. |
```abap DATA(s1) = `cheers`. IF s1 CA `aeiou`. ... "true; sy-fdpos: 2 IF s1 CA `xy`. ... "false; sy-fdpos: 6 ``` |
NA |
Contains not any To determine whether any character of a given character set is not contained in a string. See the note above. |
```abap DATA(s2) = `Hallo`. IF s2 NA `bcdeh`. ... "true; sy-fdpos: 5 IF s2 NA `bcdeH`. ... "false; sy-fdpos: 0 ``` |
CO |
Contains only To determine whether a string contains only a certain set of characters. See the note above. |
```abap DATA(s3) = `abcd`. IF s3 CO `abcd`. ... "true; sy-fdpos: 4 IF s3 CO `abCd`. ... "false; sy-fdpos: 2 ``` |
CN |
Contains not only To determine whether a string does not only contain a certain set of characters, i.e. whether a string contains characters other than those in the character set. See the note above. |
```abap DATA(s4) = `abap`. IF s4 CN `ab`. ... "true; sy-fdpos: 3 IF s4 CN `abp`. ... "false; sy-fdpos: 4 ``` |
CS |
Contains string For simple substring searches and determining whether a string contains a substring. Note: The search is not case-sensitive. sy-fdpos contains the offset of the first substring found. If it is not found, sy-fdpos contains the length of the string searched. |
```abap DATA(s5) = `Lorem ipsum dolor sit amet.`. IF s5 CS `or`. ... "true; sy-fdpos: 1 IF s5 CS `zz`. ... "false; sy-fdpos: 27 ``` |
NS |
Contains no string To determine whether a substring is not contained in a string. See the note for CS. |
```abap DATA(s6) = `some test string`. IF s6 NS `tests`. ... "true; sy-fdpos: 16 IF s6 NS `TEST`. ... "false; sy-fdpos: 5 ``` |
CP |
Conforms to pattern For simple pattern searches and determining whether a set of characters is contained in a string that matches a particular pattern. You can use the following special characters as patterns:
#. If a pattern is found, sy-fdpos returns the offset of the first occurrence. Otherwise, it contains the length of the string searched.
|
```abap DATA(s7) = `abc_def_ghi`. "Pattern: f is preceded by any character sequence, must be followed "by '_' and then followed by any character sequence IF s7 CP `*f#_*`. ... "true; sy-fdpos: 6 "Pattern: i is preceded by any character sequence, must be followed "by any character or a blank IF s7 CP `*i+`. ... "false; sy-fdpos: 11 ``` |
NP |
Does not conform to pattern Negation of CP. See the previous notes. |
```abap DATA(s8) = `abcDEFghi`. "Pattern: c is preceded by any character sequence, must be followed "by a small letter d, and then followed by any character sequence IF s8 NP `*c#d*`. ... "true; sy-fdpos: 9 "Pattern: c is preceded by any character sequence, must be followed "by a capital letter D, and then followed by any character sequence IF s8 NP `*c#D*`. ... "false; sy-fdpos: 2 ``` |
string).
``` abap
DATA(string) = `ABAP `.
"Removing trailing blanks
cl_abap_string_utilities=>del_trailing_blanks( CHANGING str = string ).
"`ABAP`
"Preserving trailing blanks when assigning text fields to data objects of
"type string
DATA(chars) = 'ABAP '.
cl_abap_string_utilities=>c2str_preserving_blanks( EXPORTING source = chars
IMPORTING dest = DATA(str_w_blanks) ).
"`ABAP `
DATA(str_no_blanks) = CONV string( chars ).
"`ABAP`
```
- `XCO_CP`: The Extension Components Library (XCO) library provides [released APIs](https://help.sap.com/doc/abapdocu_cp_index_htm/CLOUD/en-US/index.htm?file=abenreleased_api_glosry.htm) and offers various development utilities. Find more information [here](https://help.sap.com/docs/btp/sap-business-technology-platform/overview-of-xco-modules). The following code snippet demonstrates several methods of the class that deal with string processing.
```abap
"--------- Extracting a substring from a string ---------
DATA(some_string) = `abcdefghijklmnopqrstuvwxyz`.
"Creating an encapsulation of a string using XCO
DATA(str) = xco_cp=>string( some_string ).
"Using the FROM and TO methods, you can determine
"the character position. Note that the value includes the
"character at the position specified.
"The character index pattern for the example string above
"is (the string has 26 characters in total):
"a = 1, b = 2, c = 3 ... z = 26
"a = -26, b = -25, c = -24 ... z = -1
"Providing a value that is out of bounds means that
"the first (or the last) character of the string is used
"by default.
"Note: When combining FROM and TO, e.g. with method
"chaining ...->from( ...)->to( ... ), note that another
"instance is created with the first 'from', and another
"character index pattern is created based on the new
"and adjusted string value.
"bcdefghijklmnopqrstuvwxyz
DATA(sub1) = str->from( 2 )->value.
"defghijklmnopqrstuvwxyz
DATA(sub2) = str->from( -23 )->value.
"vwxyz
DATA(sub3) = str->from( -5 )->value.
"abcde
DATA(sub4) = str->to( 5 )->value.
"ab
DATA(sub5) = str->to( -25 )->value.
"Result of 1st 'from' method call: bcdefghijklmnopqrstuvwxyz
"Based on this result, the 'to' method call is
"applied.
"bcdefg
DATA(sub6) = str->from( 2 )->to( 6 )->value.
"Result of 1st 'to' method call: abcdefghijklmnopq
"Based on this result, the 'from' method call is
"applied.
"defghijklmnopq
DATA(sub7) = str->to( -10 )->from( 4 )->value.
"Values that are out of bounds.
"In the example, the first and last character of the
"string are used.
"abcdefghijklmnopqrstuvwxyz
DATA(sub8) = str->from( 0 )->to( 100 )->value.
"--------- Splitting and joining ---------
"Splitting a string into a string table
DATA(str_table) = xco_cp=>string( `Hello.World.ABAP` )->split( `.` )->value.
"Hello
"World
"ABAP
"Concatenating a string table into a string; specifying a delimiter
str_table = VALUE #( ( `a` ) ( `b` ) ( `c` ) ).
"a, b, c
DATA(conc_str1) = xco_cp=>strings( str_table )->join( `, ` )->value.
"Concatenating a string table into a string; specifying a delimiter and
"reversing the table order
"c / b / a
DATA(conc_str2) = xco_cp=>strings( str_table )->reverse( )->join( ` / ` )->value.
"--------- Prepending and appending strings ---------
DATA(name) = xco_cp=>string( `Max Mustermann` ).
"Max Mustermann, Some Street 1, 12345 Someplace
DATA(address) = name->append( `, Some Street 1, 12345 Someplace` )->value.
"Mr. Max Mustermann
DATA(title) = name->prepend( `Mr. ` )->value.
"--------- Transforming to lowercase and uppercase ---------
"ABAP
DATA(to_upper) = xco_cp=>string( `abap` )->to_upper_case( )->value.
"hallo world
DATA(to_lower) = xco_cp=>string( `HALLO WORLD` )->to_lower_case( )->value.
"--------- Checking if a string starts/ends with a specific string ---------
DATA check TYPE string.
DATA(str_check) = xco_cp=>string( `Max Mustermann` ).
"yes
IF str_check->ends_with( `mann` ).
check = `yes`.
ELSE.
check = `no`.
ENDIF.
"no
IF str_check->starts_with( `John` ).
check = `yes`.
ELSE.
check = `no`.
ENDIF.
"--------- Converting strings to xstrings using a codepage ---------
"536F6D6520737472696E67
DATA(xstr) = xco_cp=>string( `Some string` )->as_xstring( xco_cp_character=>code_page->utf_8 )->value.
"--------- Camel case compositions and decompositions with split and join operations ---------
"Pascal case is also possible
"someValue
DATA(comp) = xco_cp=>string( `some_value` )->split( `_` )->compose( xco_cp_string=>composition->camel_case )->value.
"Camel case decomposition
"some_value
DATA(decomp) = xco_cp=>string( `someValue` )->decompose( xco_cp_string=>decomposition->camel_case )->join( `_` )->value.
"--------- Matching string against regular expression ---------
DATA match TYPE string.
"yes
IF xco_cp=>string( ` 1` )->matches( `\s\d` ).
match = 'yes'.
ELSE.
match = 'no'.
ENDIF.
"no
IF xco_cp=>string( ` X` )->matches( `\s\d` ).
match = 'yes'.
ELSE.
match = 'no'.
ENDIF.
```
## Executable Example
[zcl_demo_abap_string_proc](./src/zcl_demo_abap_string_proc.clas.abap)
> **💡 Note**