Application Development and Automation 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: 
Read only

strings

Former Member
0 Likes
413

can anyone send me a program to search a word in a given string and find the no of times it has appeared in the string

2 REPLIES 2
Read only

Former Member
0 Likes
373

Hi,

See the Doc.

Syntax Diagram

SEARCH

Syntax

SEARCH dobj FOR pattern [IN {BYTE|CHARACTER}} MODE]

[STARTING AT p1] [ENDING AT p2]

[ABBREVIATED]

[AND MARK].

Extras:

1. ... IN {BYTE|CHARACTER} MODE

2. ... [STARTING AT p1] [ENDING AT p2]

3. ... ABBREVIATED

4. ... AND MARK

Effect

This statement searches the data object dobj according to the search pattern specified in pattern. The additions allow to search subareas, shortened patterns and marking of places of finding.

The search ends at the first hit and sy-fdpos is set to the offset of the found pattern or to the word in the search area. If the pattern is not found, then sy-fdpos is set to value 0.

Search Pattern in pattern

The pattern in pattern can have the following forms (Upper or lower case is irrelevant in character chain processing):

"pat"

The byte- or character chain "pat" is searched. In character chain processing, blanks at the end of the character chain are ignored and wildcard characters (*) are treated in a special way if they are found at the position of the first or last character (see following sections).

".pat."

Only valid at character chain processing. If a pattern "pat" is enclosed by dots (.) , then exactly the character sequence "pat" is searched, whereby blanks at the end are accepted and wildcard characters (*) are not treated as such.

"*pat"

Only valid at character chain processing. If a pattern contains the wildcard character (*) as first character, then a word is searched (see below) which ends with the character sequence "pat".

"pat*"

Only valid at character chain processing. If a character sequence contains the wildcard character (*) as last character, a word (see below) is searched that begins with the character sequence "pat".

"pat"

Only valid at character chain processing. If a character sequence contains the wildcard character () as first and last character, then not a word (see below) is searched that contains "pat", but a word that ends with "pat".

A word in a character-type data object dobj is defined: 1. Enclosed by non-alphanumerical separators. 2. Located at beginning or end of a row.

During string processing with data objects dobj of fixed length, the closing empty character is taken into account, while with pattern it is not. If pattern is an empty string or is of type c, d, n or t and only contains empty characters, the search is never successful.

System Fields

sy-subrc Relevance

0 The search pattern was found in dobj.

4 The search pattern was not found in dobj.

Notes

From release 6.10 on, we recommend where possible that you use the new statement FIND instead of SEARCH. Since Release 7.0, the functions of SEARCH have - with the exception of marking the found pattern (additionAND MARK) - been covered by the introduction of regular expressions into the statement FIND. If required, marking after a pattern has been found can be replaced by the statement REPLACE. Here, the replacement patterns for regular expressions are particularly useful. In contrast to FIND, SEARCH cannot distinguish between upper and lower case, and is must slower when searching large texts.

A variant of this statement used for searching tables - SEARCH itab - has also been replaced by a variant of statement FIND.

Addition 1

... IN {BYTE|CHARACTER} MODE

Effect

The optional addition IN { BYTE | CHARACTER } MODE determines whether byte- or character chain processing will be executed. If the addition is not specified, character chain processing will be executed. Depending on the processing type, dobj and pattern must be either byte- or character-type.

Addition 2

... [STARTING AT p1] [ENDING AT p2]

Effect

With the additions STARTING AT and ENDING AT, you can restrict the search to a subarea of the data object dobj. For p1 and p2, data objects of the data objects of the data type i are expected.

The value in p1 specifies the first, the value in p2 specifies the last of the positions to be searched. Without specifiying STARTING AT p1, the data object dobj is searched from the first position to position p2. Without specifying ENDING AT p2, dobj is searched from position p1 to the end.

If the addition STARTING AT is specified, then sy-fdpos is set to the offset of the find location minus the offset of p1, provided that the search was successful. In the following cases, the search is not carried out, and sy-subrc is set to 4:

The value of p1 or p2 is smaller than 1.

The value of p1 is larger than the length of dobj.

The value of p2 is smaller than or equal to p1.

Note

The term "position" is not equivalent to the term "offset". A byte or a character on position 1 has an offset of 0.

Addition 3

... ABBREVIATED

Effect

With the addition ABBREVIATED, you can specify a shortened pattern in pattern. This addition is only possible with character chain processing. A word is searched in dobj that begins with the same character as the pattern in pattern and contains the remaining characters of "pattern" in the same sequence, but at otherwise completely arbitrary positions of the word.

Example

Search for an abbreviated pattern with SEARCH. The FIND statement has the same result and also specifies the find location.

DATA: text TYPE string VALUE `Roll over Beethoven`,

moff TYPE i,

mlen TYPE i.

SEARCH text FOR 'bth' ABBREVIATED.

FIND REGEX '\<(b[a-z0-9]t[a-z0-9]h[a-z0-9]*)\>' IN text

IGNORING CASE

MATCH OFFSET moff

MATCH LENGTH mlen.

Addition 4

... AND MARK

Effect

With the addition AND MARK, a character sequence found in dobj or a found word, is converted to upper case. This addition is only possible with character chain processing, and you are only allowed to specify changeable data objects for dobj when you use it.

Example

The first two SEARCH-statements have the same effect. They find the first blank in text and set sy-fdpos to the value 4. The third SEARCH-statement finds the word "Beethoven" in the search area (beginning from position 6 of text ), sets sy-fdpos to the value 5, i.e., the offset of the place of finding in the search area and changes the content of text to "Roll over BEETHOVEN" .

DATA: text TYPE string VALUE `Roll over Beethoven`,

pos TYPE i.

SEARCH text FOR '. .'.

SEARCH text FOR ` `.

IF sy-subrc = 0.

pos = sy-fdpos + 2.

SEARCH text FOR 'bth' STARTING AT pos

ABBREVIATED AND MARK.

ENDIF.

Regards,

Anji

Read only

Former Member
0 Likes
373

YOu can use the FIND or SEARCH syntax.

Type FIND or SEARCH in abap editor and press f1 for sample usage of the syntax.

Regards,

Ravi