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

Former Member
0 Likes
3,980

Hi All,

I am trying to get rid of single quotes ie, ', from my text field and replace it with blank, any idea how the code should look like, double quotes have no issues , '"', works but what to write anout single quotes,''' doesnt work.

Sorry , if it sounds very easy, may be I am missing out soemthing.

Thanks in Advance

Tina

1 ACCEPTED SOLUTION
Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
2,538

Hi Tina, ABAP is a little funny when dealing with single quotes. Check the following program, notice that for one single 'quote, we need to use 2 and then of course wrap with quotes which give you a total of 4.



report zrich_0001.


data:  text type string.

text = 'This is a string with a '' in it, lets get rid of it'.


replace '''' with space into text.


write:/ text.

Regards,

Rich Heilman

19 REPLIES 19
Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
2,539

Hi Tina, ABAP is a little funny when dealing with single quotes. Check the following program, notice that for one single 'quote, we need to use 2 and then of course wrap with quotes which give you a total of 4.



report zrich_0001.


data:  text type string.

text = 'This is a string with a '' in it, lets get rid of it'.


replace '''' with space into text.


write:/ text.

Regards,

Rich Heilman

Read only

Former Member
0 Likes
2,538

HI,

see the below program

REPORT  Y_TEST.

data: text(10).
text =  'sudh''eer'.
write: text.

replace '''' with SPACE into text.

write:/ text.

Thanks

Sudheer

Read only

Former Member
0 Likes
2,538

Hey , what if my character i 'subh'eer'?

and I want to get rid of ' in the middle.

thanks in advance.

Read only

0 Likes
2,538

In the ABAP command if you wish to use single quotes a single quote previous to it indicates that you wish to enter a single quote.

REPLACE l_string with ''' '.

means replacing the first occurence of single quote to space. Surely if you wish to replace mulitple occurence better use TRANSLATE.

Regards

Anurag

Read only

0 Likes
2,538

hi tina,

try using the Translate command with which u can replace all the occurences..

Read only

Former Member
0 Likes
2,538

Hi tina,

if u want to pass a string with single quote to a variable say 'text's' to v_data it considers the first occurence of ' after t as the end of the text and it gives u a syntax error.

data: v_data type string value 'text's'.

error: unable to interpret s after '.

hope this helps.

regards,

keerthi.

Read only

Former Member
0 Likes
2,538

Hi All ,

thanks for your help.

I meant my work area is holding value like

wa = "ajsja"xaj'asa . These are all values.

if wa-txt ca '"'.

REPLACE all occurrences of '"' in wa-txt WITH space.

endif.

if wa-txt ca `'`.

REPLACE all occurrences of `'` in wa-txt WITH space.

endif.

Shoud this not work.?

Read only

0 Likes
2,538

I feel the below would give syntax error.

if wa-txt ca `'`.

REPLACE all occurrences of `'` in wa-txt WITH space.

endif.

try using

if wa-txt ca `''`.

REPLACE all occurrences of `''` in wa-txt WITH space.

endif.

Regards

Anurag

Read only

0 Likes
2,538

No, the second statement will not work. You must have a total of 4 quotes there. The first one where you are looking for double quotes will work fine. The second must be like this.

if wa-txt ca ''''.
REPLACE all occurrences of '''' in wa-txt WITH space.
endif.

Regards,

Rich Heilman

Read only

Former Member
0 Likes
2,538

Hi,

This statement works, the second one is tild ` and not', also I ran in debug mode, one issue is it does not insert spaces , it remves the character just. I actually wanted aga'aah to aga aah.

Read only

0 Likes
2,538

You can not use tild, you must code it like above with all quotes, 4 of them.

Regards,

Rich Heilman

Read only

0 Likes
2,538

Please use the following if you want to keep the space left vacant by the quote.



if wa-txt ca ''''.
translate wa-txt using ''' '.
endif.

Regards,

Rich Heilman

Read only

0 Likes
2,538

Take this program for example, it remove both single quotes and double quotes and leaves the spaces.



report  zrich_0002.

data: wa-txt type string.

wa-txt = '"This" aga''aah'.

if wa-txt ca '"'.
  translate wa-txt using '" '.
endif.


if wa-txt ca ''''.
  translate wa-txt using ''' '.
endif.

write:/ wa-txt.


Regards,

Rich Heilman

Read only

0 Likes
2,538

Ok, so tild doesn't work in 46c, but it definitly does work in a WebAS 7.0. Just tested, it works.



report  zrich_0002.

data: wa-txt type string.

wa-txt = `"This" aga'aah`.

if wa-txt ca `"`.
  translate wa-txt using `" `.
endif.


if wa-txt ca `'`.
  translate wa-txt using `' `.
endif.

write:/ wa-txt.


Regards,

Rich Heilman

Read only

0 Likes
2,538

Ok...it is nice to understand that ur prb is solved.

Regards

Anurag

Read only

Former Member
0 Likes
2,538

Anurag,

But I need to replace both " and ', so '"' is fine and for the other `'` should work.

thanks

Read only

Former Member
0 Likes
2,538

Rich,

In your example :

First one with out tild, if my character is '"This" aga'aah', its gives error.

Read only

0 Likes
2,538

What is the error?

Regards,

Rich Heilman

Read only

Former Member
0 Likes
2,538

Hi,

The literals can be assigned only

1)between 2 single Quotes or as you have explored

2)between 2 tildas,

eg: 'HIII SSSSS'.

or HIII SSSSS.

Now When u r assigning a string which contains single qoutes <b>(but not a pair of consecutive single qoutes )</b>say <HII' SS'> then the assignment is not possible by the first method as the compiler ont be able distinguish the actual end of the Assignment.It can be done by using the second method(As Rich had shown).

But When the string contains a pair of 2 consecutive single qoutes like <HI THIS IS ''SDN''>

then it can be done using the first Method.The actual data in the Variable will be then,

THIS IS 'SDN'.

If u use the second Method,the actual data will be

THIS IS ''SDN''.

The `` tells the compiler to consider the entire text as the contents.

Regards,

Samson

Message was edited by: Samson Moses