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: 

ABAP search and replace possible ?

Former Member
0 Kudos
4,483

HI,

i have 2 Strings.

String1= "this is a test 12345 and i need this"

String2= "12345"

Is there a function or SAP module which search/replaces ?

I want to search in String1 for String2 and replace it/delete it.

ideas?

4 REPLIES 4

former_member189059
Active Contributor
0 Kudos
574
replace all occurrences of string2 in string1 with ''.

former_member404244
Active Contributor
0 Kudos
574

Hi,

use the statemnet

replace all occurrences

Regards,

Nagaraj

Former Member
0 Kudos
574

Hi,

To search a character field for a particular pattern, use the SEARCH statement as follows:

SEARCH <c> FOR <str> <options>.

The statement searches the field <c> for <str> starting at position <n1>. If successful, the return code value of SY-SUBRC is set to 0 and SY-FDPOS is set to the offset of the string in the field <c>. Otherwise, SY-SUBRC is set to 4.

The search string <str> can have one of the following forms:

<str> - Function

<pattern> - Searches for <pattern> (any sequence of characters). Trailing blanks are ignored.

.<pattern>. - Searches for <pattern>. Trailing blanks are not ignored.

*<pattern> - A word ending with <pattern> is sought.

<pattern>* - Searches for a word starting with <pattern>.

Words are separated by blanks, commas, periods, semicolons, colons, question marks, exclamation marks, parentheses, slashes, plus signs, and equal signs.

<option> in the SEARCH FOR statement can be any of the following:

ABBREVIATED

Searches the field <c> for a word containing the string in <str>. The characters can be separated by other characters. The first letter of the word and the string <str> must be the same.

STARTING AT <n1>

Searches the field <c> for <str> starting at position <n1>. The result SY-FDPOS refers to the offset relative to <n1> and not to the start of the field.

ENDING AT <n2>

Searches the field <c> for <str> up to position <n2>.

AND MARK

If the search string is found, all the characters in the search string (and all the characters in between when using ABBREVIATED) are converted to upper case.

Ex.

DATA STRING(30) VALUE 'This is a little sentence.'.

WRITE: / 'Searched', 'SY-SUBRC', 'SY-FDPOS'.

ULINE /1(26).

SEARCH STRING FOR 'X'.

WRITE: / 'X', SY-SUBRC UNDER 'SY-SUBRC',

SY-FDPOS UNDER 'SY-FDPOS'

SEARCH STRING FOR 'itt '.

WRITE: / 'itt ', SY-SUBRC UNDER 'SY-SUBRC',

SY-FDPOS UNDER 'SY-FDPOS'.

SEARCH STRING FOR '.e .'.

WRITE: / '.e .', SY-SUBRC UNDER 'SY-SUBRC',

SY-FDPOS UNDER 'SY-FDPOS'.

SEARCH STRING FOR '*e'.

WRITE: / '*e ', SY-SUBRC UNDER 'SY-SUBRC',

SY-FDPOS UNDER 'SY-FDPOS'.

SEARCH STRING FOR 's*'.

WRITE: / 's* ', SY-SUBRC UNDER 'SY-SUBRC',

SY-FDPOS UNDER 'SY-FDPOS'.

Regards,

Bhaskar

Former Member
0 Kudos
574

cONSIDER THE CODE BELOW, I hope it serves your purpose

data: string1(36),string2(5).

String1 = 'this is a test 12345 and i need this'.

String2 = '12345'.

*To replace '12345' with 'ABCDE'

replace all occurrences of string2 in string1 with 'ABCDE'.

WRITE:/ sTRING1.

*To delete 'ABCDE'

String2 = 'ABCDE'.

replace all occurrences of string2 in string1 with ''.

WRITE:/ sTRING1.

Reward points if useful, get back in case of query...

Cheers!!!