comparing strings if co (sap library - abap programming (bc-aba))

3
27/02/13 Comparing Strings (SAP Library - ABAP Programming (BC-ABA)) help.sap.com/saphelp_nw04/Helpdata/EN/fc/eb3516358411d1829f0000e829fbfe/content.htm 1/3 Comparing Strings 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

Upload: hjheredias

Post on 18-Jul-2016

21 views

Category:

Documents


0 download

DESCRIPTION

Comparing StringsSimilarly to the special statements for processing strings, there are special comparisons that youcan apply to strings with types C, D, N, and T. You can use the following operators: MeaningCO Contains OnlyCN Contains Not onlyCA Contains AnyNA contains Not AnyCS Contains StringNS contains No StringCP Matches patternNP Does not match patternThere are no conversions with these comparisons. Instead, the system compares the characters of the string. Theoperators have the following functions:CO (Contains Only)The logical expression CO is true if contains only characters from . The comparison is case-sensitive. Trailing blanks are included. Ifthe comparison is true, the system field SY-FDPOS contains the length of . If it is false, SY-FDPOS containsthe offset of the first character of that does not occur in .CN (Contains Not only)The logical expression CN is true if does also contains characters other than those in . The comparison is casesensitive.Trailing blanks are included. If the comparison is true, the system field SY-FDPOScontains the offset of the first character of that does not also occur in . If it is false, SYFDPOScontains the length of .CA (Contains Any)The logical expression CA is true if contains at least one character from . The comparison is case-sensitive. If thecomparison is true, the system field SY-FDPOS contains the offset of the first character of that also occurs in . If it is false, SY-FDPOS contains the length of .NA (contains Not Any)The logical expression NA is true if does not contain any character from . The comparison is case-sensitive. If thecomparison is true, the system field SY-FDPOS contains the length of . If it is false, SY-FDPOScontains the offset of the first character of that occurs in .CS (Contains String)The logical expression CS is true if contains the string . Trailing spaces are ignored and the comparison is not27/02/13 Comparing Strings (SAP Library - ABAP Programming (BC-ABA))help.sap.com/saphelp_nw04/Helpdata/EN/fc/eb3516358411d1829f0000e829fbfe/content.htm 2/3case-sensitive. If the comparison is true, the system field SY-FDPOS contains the offset of in . If it is false, SY-FDPOS contains the length of .NS (contains No String)The logical expression NS is true if does not contain the string . Trailing spaces are ignored and the comparison isnot case-sensitive. If the comparison is true, the system field SY-FDPOS contains the length of. If it is false, SY-FDPOS contains the offset of in .CP (Contains Pattern)The logical expression CP is true if matches the pattern . If is of type C, you can use the following wildcards in :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 in . If it is false, SY-FDPOS containsthe length of .If you want to perform a comparison on a particular character in , place the escape character #in front of it. You can use the escape character # to specifycharacters in upper and lower casethe 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 NP is true if does not match the pattern . In , you can use the same wildcards and escape character asfor 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 . If it is false, SY-FDPOS contains the offsetof

TRANSCRIPT

Page 1: Comparing Strings if CO (SAP Library - ABAP Programming (BC-ABA))

27/02/13 Comparing Strings (SAP Library - ABAP Programming (BC-ABA))

help.sap.com/saphelp_nw04/Helpdata/EN/fc/eb3516358411d1829f0000e829fbfe/content.htm 1/3

Comparing Strings 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

Page 2: Comparing Strings if CO (SAP Library - ABAP Programming (BC-ABA))

27/02/13 Comparing Strings (SAP Library - ABAP Programming (BC-ABA))

help.sap.com/saphelp_nw04/Helpdata/EN/fc/eb3516358411d1829f0000e829fbfe/content.htm 2/3

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> .

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

Page 3: Comparing Strings if CO (SAP Library - ABAP Programming (BC-ABA))

27/02/13 Comparing Strings (SAP Library - ABAP Programming (BC-ABA))

help.sap.com/saphelp_nw04/Helpdata/EN/fc/eb3516358411d1829f0000e829fbfe/content.htm 3/3

'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