Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Restrict Special Characteristics

Former Member
0 Kudos

HI Experts,

How to restrict special characters in character string. field was alphanumeric only.

thanks in advance

Siva

4 REPLIES 4

Former Member
0 Kudos

HI,

use string condition with CO

Process:

Step1

take a string say str1 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqust0123456789'.

Step 2: Set conditon as

If STR CO str1.      (Here STR is your string)

  (do your operation).

else.
 error message

endif.

for further study , you may check

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

regards,

Anirban

Former Member
0 Kudos

Hi siva ,

You can use string comparision operaters.

Here is the help from sap.

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

'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

Here is sample useful program to your problem.

data:

w_data1(30) type c,

w_data2(30) type c value '$ is special symbol'.

concatenate sy-abcde '012345678' into w_data1.

if w_data2 co w_data1.

write:

/ 'There are no speacial characters'.

else.

write:

/ 'There are speacial characters in the given string'.

endif.

Hope this helps you.

Regards,

Rama.

Former Member
0 Kudos

Use This Code to Find out Special Character

<STRING> CA '~`!@#$%^&*()-_+=<>,.?/:;?'.

.

Former Member
0 Kudos

Hi try this,

Append all spl chars to an internal table.

Loop at <itab>

SEARCH <string> FOR <itab-Spl chars>.

The return code is set as follows:

SY-SUBRC = 0:

The search string g was found in the field f. SY-FDPOS contains the offset of the found string or the found word within the field.

SY-SUBRC = 4:

The search string g was not found in the field f.

if sy-subrc EQ 0.

<< an occurence found>>

***display error message or set a flag

else.

<<<continue with your code>>or set a different flag

endif.

endloop.

Regards,

Linda.