The following table shows the relational operators for comparisons between character-like operands (single data objects or return values or string expressions) in comparison expressions.
The trailing blanks are respected for operands of type string. If not stated differently in the following table, the trailing blanks are ignored for operands of types c, d, n, and t.
operator | Meaning |
CO | Contains Only: True, if operand1 only contains characters from operand2. It is case-sensitive and trailing blanks are respected in both operands. If operand2 is of type string and initial, then the relational expression is false, except if operand1 is also of type string and initial. If operand1 has the type string and is initial, the relational expression is always true, regardless of operand2. If the comparison is true, sy-fdpos contains the offset of the first character in operand1 that is not contained in operand2. If the comparison is true, sy-fdpos contains the length of operand1. |
CN | Contains Not Only: True if a relational expression with CO is false, that is, if operand1 contains not only characters from operand2. sy-fdpos is set in the same way as for CO. If the comparison is true, sy-fdpos contains the offset of the first character in operand1 that is not contained in operand2. If the comparison is false, sy-fdpos contains the length of operand1. |
CA | Contains Any: True, if operand1 contains at least one character from operand2. It is case-sensitive and trailing blanks are respected in both operands. If operand1 or operand2 are of the type string and initial, the relational expression is always false. If result of the comparison is positive, sy-fdpos contains the offset of the first character in operand1 that is also contained in operand2. If the comparison is false, sy-fdpos contains the length of operand1. |
NA | Contains Not Any: True, if a relational expression with CA is false, that is if operand1 does not contain any characters from operand2. If the comparison is false, sy-fdpos contains the offset of the first character in operand1 that is also contained in operand2. If the comparison is true, sy-fdpos contains the length of operand1. |
CS | Contains String: True, if the content of operand2 is contained in operand1. It is not case-sensitive and trailing blanks in the left operand are respected. If operand1 is of type string and initial, or of type c and contains only blank characters, the relational expression is false, unless operand2 is also of type string and initial, or of type c and only contains blank characters. In this case, the relational expression is always true. If the comparison is true, sy-fdpos contains the offset of operand2 in operand1. If the comparison is false, sy-fdpos contains the length of operand1. |
NS | Contains No String: True, if a relational expression with CS is false, that is if operand1 does not contain the content of operand2. If the comparison is false, sy-fdpos contains the offset of operand2 in operand1. If the comparison is true, sy-fdpos contains the length of operand1. |
CP | Covers Pattern: True, if the content of operand1 fits the pattern in operand2. Wildcard characters can be used to create the operand2 pattern, where “*” represents any character string (including a blank string) and “+” represents any character. It is not case-sensitive. Trailing blanks in the left operand are respected. If the comparison is true, sy-fdpos contains the offset of operand2 in operand1, whereby leading wildcard characters “*” in operand2 are ignored if operand2 also contains other characters. If the comparison is false, sy-fdpos contains the length of operand1. Characters in operand2 can be selected for direct comparisons by prefixing them with the escape character“#”. For characters flagged in this way in operand2, the operator is case-sensitive. Also, wildcard characters and the escape character are not subject to special handling and trailing blanks are relevant. |
NP | No Pattern: True, if a relational expression with CP is false, that is, if operand1 does not fit the pattern operand2. If the comparison is false, sy-fdpos contains the offset of operand2 in operand1, whereby leading wildcard characters “*” in operand2 are ignored if operand2 also contains other characters. If the comparison is true, sy-fdpos contains the length of operand1. |
Notes
- The operators CP and NP use multiple wildcard characters “*” in a row in the same way as a single “*” character. The wildcard character “+” does not represent a blank string.
- Operands of byte-like data types can only be compared with the relational operators in this table outside of Unicode programs. To execute the corresponding comparisons for byte-like operands in Unicode programs, the relational operators for byte-like data types can be used.
- The relational operators in this table can be replaced by predicate functions.
- The statement FIND and the search functions find can be quicker than the relational operator CS by some magnitude.
Example
Searches for HTML tags in a text using operator CP. This search finds the first HTML tag “<i>” at offset 8. Note that it is not enough to specify a search pattern “<*>”, since CP stands for Covers Pattern, and not for Contains Pattern. The example also shows that leading wildcard characters “*” in the string are ignored in sy-fdpos so that the occurrence found by this search can be identified.
DATA html TYPE string.
html = `This is <i>italic</i>!`.
IF html CP ‘*<*>*’.
cl_demo_output=>display_text( |Found HTML tag at { sy-fdpos }| ).
ENDIF.
以上字符串的比较,都是有返回sy-fdpos
CS在很多的时候可以看成SEARCH/FIND使用,但是对特殊字符,CS要优先于其他。
原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/18661.html