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

Replacing a special character in a string with another string

Former Member
0 Likes
2,891

Hi

I need to replace a special character in a string with another string.

Say there is a string - "abc's def's are alphabets"

and i need to replace all the ' (apostrophe) with &apos& ..which should look like as below

"abc&apos&s def&apos&s are alphabets" .

Kindly let me know how this requirement can be met.

Regards

Sukumari

Hi

I need to replace a special character in a string with another string.

Say there is a string - "abc's def's are alphabets"

and i need to replace all the ' (apostrophe) with &apos& ..which should look like as below

"abc&apos&s def&apos&s are alphabets" .

Kindly let me know how this requirement can be met.

Regards

Sukumari

5 REPLIES 5
Read only

Former Member
0 Likes
1,331

der is a command called

REPLACE ALL OCCURENCES OF

use it ur pupose..

REPLACE ALL OCCURRENCES OF ''' IN:

text1 WITH '&apos&'.

This will help..

Reward if it does

Message was edited by:

Prashant H Gupta

Read only

Former Member
0 Likes
1,331

REPLACE

Syntax Forms

Pattern-based replacement

1. REPLACE [{FIRST OCCURRENCE}|{ALL OCCURRENCES} OF]

pattern

IN [section_of] dobj WITH new

[IN {BYTE|CHARACTER} MODE]

[{RESPECTING|IGNORING} CASE]

[REPLACEMENT COUNT rcnt]

{ {[REPLACEMENT OFFSET roff]

[REPLACEMENT LENGTH rlen]}

| [RESULTS result_tab|result_wa] }.

Position-based replacement

2. REPLACE SECTION [OFFSET off] [LENGTH len] OF dobj WITH new

[IN {BYTE|CHARACTER} MODE].

Effect

This statement replaces characters or bytes of the variable dobj by characters or bytes of the data object new. Here, position-based and pattern-based replacement are possible.

When the replacement is executed, an interim result without a length limit is implicitly generated and the interim result is transferred to the data object dobj. If the length of the interim result is longer than the length of dobj, the data is cut off on the right in the case of data objects of fixed length. If the length of the interim result is shorter than the length of dobj, data objects of fixed length are filled to the right with blanks or hexadecimal zeroes. Data objects of variable length are adjusted. If data is cut off to the right when the interim result is assigned, sy-subrc is set to 2.

In the case of character string processing, the closing spaces are taken into account for data objects dobj of fixed length; they are not taken into account in the case of new.

System fields

sy-subrc Meaning

0 The specified section or subsequence was replaced by the content of new and the result is available in full in dobj.

2 The specified section or subsequence was replaced in dobj by the contents of new and the result of the replacement was cut off to the right.

4 The subsequence in sub_string was not found in dobj in the pattern-based search.

8 The data objects sub_string and new contain double-byte characters that cannot be interpreted.

Note

These forms of the statement REPLACE replace the following obsolete form:

REPLACE sub_string WITH

Syntax

REPLACE sub_string WITH new INTO dobj

[IN {BYTE|CHARACTER} MODE]

[LENGTH len].

Extras:

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

2. ... LENGTH len

Effect

This statement searches through a byte string or character string dobj for the subsequence specified in sub_string and replaces the first byte or character string in dobj that matches sub_string with the contents of the data object new.

The memory areas of sub_string and new must not overlap, otherwise the result is undefined. If sub_string is an empty string, the point before the first character or byte of the search area is found and the content of new is inserted before the first character.

During character string processing, the closing blank is considered for data objects dobj, sub_string and new of type c, d, n or t.

System Fields

sy-subrc Meaning

0 The subsequence in sub_string was replaced in the target field dobj with the content of new.

4 The subsequence in sub_string could not be replaced in the target field dobj with the contents of new.

Note

This variant of the statement REPLACE will be replaced, beginning with Release 6.10, with a new variant.

Addition 1

... IN {BYTE|CHARACTER} MODE

Effect

The optional addition IN {BYTE|CHARACTER} MODE determines whether byte or character string processing will be executed. If the addition is not specified, character string processing is executed. Depending on the processing type, the data objects sub_string, new, and dobj must be byte or character type.

Addition 2

... LENGTH len

Effect

If the addition LENGTH is not specified, all the data objects involved are evaluated in their entire length. If the addition LENGTH is specified, only the first len bytes or characters of sub_string are used for the search. For len, a data object of the type i is expected.

If the length of the interim result is longer than the length of dobj, data objects of fixed length will be cut off to the right. If the length of the interim result is shorter than the length of dobj, data objects of fixed length are filled to the right with blanks or with hexadecimal 0. Data objects of variable length are adapted.

Example

After the replacements, text1 contains the complete content "I should know that you know", while text2 has the cut-off content "I should know that".

DATA: text1 TYPE string VALUE 'I know you know',

text2(18) TYPE c LENGTH 18 VALUE 'I know you know',

sub_string TYPE string VALUE 'know',

new TYPE string VALUE 'should know that'.

REPLACE sub_string WITH new INTO text1.

REPLACE sub_string WITH new INTO text2.

Read only

Former Member
0 Likes
1,331

HI,

REPLACE ALL OCCURRENCES OF '''' IN string WITH '&apos&'.

rgs

Read only

Former Member
0 Likes
1,331

Hi,

try this,

REPLACE ALL OCCURENCES OF ''' in var1 WITH '&apos&'.

where,

var1 is the variable that contains the string..

Read only

Former Member
0 Likes
1,331

Hi,

You can use <b>Replace</b> stmt for this.