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

How to remove one char from string and replace it with SPACE.

Former Member
0 Likes
4,615

My requirement is to remove a defined 1 char (on a known position) from string and replace it with SPACE. The below code is working fine, but it is replacing the char with 'x'.

DATA: text1 TYPE string.
text1  = 'abcdXefgh'.
REPLACE SECTION OFFSET 4 LENGTH 1 OF:
                text1 WITH 'x'.
WRITE: / text1.

The below code is not working fine since it is removing also the SPACE.

DATA: text1 TYPE string.
text1  = 'abcdXefgh'.
REPLACE SECTION OFFSET 4 LENGTH 1 OF:
                text1 WITH space.
WRITE: / text1.

Whats the best way to remove the char and replace it with space?

1 ACCEPTED SOLUTION
Read only

Sandra_Rossi
Active Contributor
4,117

Another possible solution (use backquotes to indicate a true space character, because in lots of places SPACE is like a null-length string, not a space character - I like my clear explanations 🙂 😞

REPLACE SECTION OFFSET 4 LENGTH 1 OF:
                text1 WITH ` `.

My requirement is to remove a defined 1 char (on a known position) from string and replace it with SPACE. The below code is working fine, but it is replacing the char with 'x'.

DATA: text1 TYPE string.
text1  = 'abcdXefgh'.
REPLACE SECTION OFFSET 4 LENGTH 1 OF:
                text1 WITH 'x'.
WRITE: / text1.

The below code is not working fine since it is removing also the SPACE.

DATA: text1 TYPE string.
text1  = 'abcdXefgh'.
REPLACE SECTION OFFSET 4 LENGTH 1 OF:
                text1 WITH space.
WRITE: / text1.

Whats the best way to remove the char and replace it with space?

4 REPLIES 4
Read only

FredericGirod
Active Contributor
4,117

You have to work with string, not text.

My "not terrible" solution

data(text1)  = 'abcdefgh'.


data offset type i value 4.
data size   type i value 1.


data(second_offset) = offset + 1.
data(second_size)   = strlen(  text1 ) - ( offset + 1 ).




data(text2) = |{ text1+0(offset) } { text1+second_offset(second_size) }|.
write text2.
Read only

former_member610335
Participant
4,117

Another possible easier solution:

DATA(line) = 'abcdef'.
WRITE / line.
line+3(1) = SPACE.
WRITE / line.

Read only

Sandra_Rossi
Active Contributor
4,118

Another possible solution (use backquotes to indicate a true space character, because in lots of places SPACE is like a null-length string, not a space character - I like my clear explanations 🙂 😞

REPLACE SECTION OFFSET 4 LENGTH 1 OF:
                text1 WITH ` `.
Read only

4,117

Simple and precise solution by Sandra.
You are not the only one to like your explanation, I like it too. :-))