Comparing Strings


  Locate the document in its SAP Library structure
Similarly to the special statements for processing strings, there are special comparisons that you can apply to strings with types C, D, N, and T. You can use the following operators:
<operator>
Meaning
CO
Contains Only
CN
Contains Not only
CA
Contains Any
NA
contains Not Any
CS
Contains String
NS
contains No String
CP
Matches pattern
NP
Does not match pattern
There are no conversions with these comparisons. Instead, the system compares the characters of the string. The operators have the following functions: CO (Contains Only) The logical expression <f1> CO <f2> is true if <f1> contains only characters from <f2>. The comparison is case-sensitive. Trailing blanks are included. If the comparison is true, the system field SY-FDPOS contains the length of <f1>. If it is false, SY-FDPOS contains the offset of the first character of <f1> that does not occur in <f2>. CN (Contains Not only) The logical expression <f1> CN <f2> is true if <f1> does also contains characters other than those in <f2>. The comparison is case-sensitive. Trailing blanks are included. If the comparison is true, the system field SY-FDPOS contains the offset of the first character of <f1> that does not also occur in <f2>. If it is false, SY-FDPOS contains the length of <f1>.
CA (Contains Any) The logical expression <f1> CA <f2> is true if <f1> contains at least one character from <f2>. The comparison is case-sensitive. If the comparison is true, the system field SY-FDPOS contains the offset of the first character of <f1> that also occurs in <f2> . If it is false, SY-FDPOS contains the length of <f1>.
NA (contains Not Any) The logical expression <f1> NA <f2> is true if <f1> does not contain any character from <f2>. The comparison is case-sensitive. If the comparison is true, the system field SY-FDPOS contains the length of <f1>. If it is false, SY-FDPOS contains the offset of the first character of <f1> that occurs in <f2> .
CS (Contains String) The logical expression <f1> CS <f2> is true if <f1> contains the string <f2>. Trailing spaces are ignored and the comparison is not case-sensitive. If the comparison is true, the system field SY-FDPOS contains the offset of <f2> in <f1> . If it is false, SY-FDPOS contains the length of <f1>.
NS (contains No String) The logical expression <f1> NS <f2> is true if <f1> does not contain the string <f2>. Trailing spaces are ignored and the comparison is not case-sensitive. If the comparison is true, the system field SY-FDPOS contains the length of <f1>. If it is false, SY-FDPOS contains the offset of <f2> in <f1> .
CP (Contains Pattern) The logical expression <f1> CP <f2> is true if <f1> matches the pattern <f2>. If <f2> is of type C, you can use the following wildcards in <f2>:

  • for any character string: *
  • for any single character: +
Trailing spaces are ignored and the comparison is not case-sensitive. If the comparison is true, the system field SY-FDPOS contains the offset of <f2> in <f1> . If it is false, SY-FDPOS contains the length of <f1>.
If you want to perform a comparison on a particular character in <f2>, place the escape character # in front of it. You can use the escape character # to specify
  • characters in upper and lower case
  • the wildcard character "*" (enter:#*)
  • the wildcard character "+" (enter: #+ )
  • the escape symbol itself (enter: ## )
  • blanks at the end of a string (enter: #___ )
NP (contains No Pattern) The logical expression <f1> NP <f2> is true if <f1> does not match the pattern <f2>. In <f2>, you can use the same wildcards and escape character as for the operator CP.Trailing spaces are ignored and the comparison is not case-sensitive. If the comparison is true, the system field SY-FDPOS contains the length of <f1>. If it is false, SY-FDPOS contains the offset of <f2> in <f1> .

Example
DATA: f1(5) TYPE c VALUE <f1>,
      f2(5) TYPE c VALUE <f2>.
IF f1 <operator> f2.
   WRITE: /  'Vergleich wahr, SY-FDPOS=', sy-fdpos.
ELSE.
   WRITE: /  'Vergleich falsch, SY-FDPOS=', sy-fdpos.
ENDIF.
The following table shows the results of executing this program, depending on which operators and values of F1 and F2.

<f1>
<operator>
<f2>
Result
SY-FDPOS
'BD '
CO
'ABCD '
true
5
'BD '
CO
'ABCDE'
false
2
'ABC12'
CN
'ABCD '
true
3
'ABABC'
CN
'ABCD '
false
5
'ABcde'
CA
'Bd '
true
1
'ABcde'
CA
'bD '
false
5
'ABAB '
NA
'AB '
false
0
'ababa'
NA
'AB '
true
5
'ABcde'
CS
'bC '
true
1
'ABcde'
CS
'ce '
false
5
'ABcde'
NS
'bC '
false
1
'ABcde'
NS
'ce '
true
5
'ABcde'
CP
'*b*'
true
1
'ABcde'
CP
'*#b*'
false
5
'ABcde'
NP
'*b*'
false
1
'ABcde'
NP
'*#b*'
true
5

No comments:

Post a Comment