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 stings within strings.

Former Member
0 Likes
1,498

hi..

I want to convert all all commas with in quotes in a string to a different symbol, say a hash. I cannot arrive at the code to do the same. Can anyone help.

Eg : input = ' 5646," Name1, Name2 ", 234324 ," comp name , city " , 435345 '.

Output should be ' 5646," Name1# Name2 ", 234324 ," comp name # city " , 435345 '.

Is this possible ?

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,467

same as some of our friend told...



DATA:in_put TYPE string.

in_put = '5646," Name1, Name2 ", 234324 ," comp name , city " , 435345'.
write : / in_put.
REPLACE ALL OCCURRENCES OF ',' IN in_put WITH '#'.

WRITE: / in_put.

even though if u are not satisfied...... U need to loop at the string "in_put "...

its like...


data: b type int4.
b = strlen( a ).
do b times.

 if a+k(1) = '"'.
   x+k(1) = '!'.

so on...
enddo.

14 REPLIES 14
Read only

Former Member
0 Likes
1,467

Hi,

Try REPLACE ALL OCCURANCES OF <str1> WITH <str2.>

Read only

0 Likes
1,467

how can i use all occurrences, i tried it initially and it doesn give the output. plz note the string doesnot have a definite pattern, i just want to replace all commas within quotes.

Read only

Former Member
0 Likes
1,467

HI,

Try this

REPLACE ALL OCCURRENCES OF ',' IN P_STRING WITH '#'.

Read only

kesavadas_thekkillath
Active Contributor
0 Likes
1,467

You can .. must break ur head

Read only

Former Member
0 Likes
1,467

Hi,

Use this logic,

loop at itab.

if itab-field CO '+-*/$@^&()<>&~.!,' .
replace all occurrences of ',' in itab-field with '#'.

write: / itab-field.
endif.
endloop.

Regards,

Nikhil.

Edited by: Nikhil Kanegaonkar on May 29, 2009 12:40 PM

Edited by: Nikhil Kanegaonkar on May 29, 2009 12:41 PM

Read only

former_member188829
Active Contributor
0 Likes
1,467

hi,

DATA:in_put TYPE string.

in_put = '5646," Name1, Name2 ", 234324 ," comp name , city " , 435345'.

REPLACE ALL OCCURRENCES OF ',' IN in_put WITH '#'.

WRITE:in_put.

Read only

0 Likes
1,467

do u know the specific number of quotes u will get in the string?

if yes..

then split the string at "

then replace use replace all occurrences of ',' with '#' on those parts u need.

then concatenate again.

Read only

0 Likes
1,467

hi vishnu,

The comma between doble quotes has to be replaced

Read only

0 Likes
1,467

Sorry.It was my mistake.

Read only

Former Member
0 Likes
1,468

same as some of our friend told...



DATA:in_put TYPE string.

in_put = '5646," Name1, Name2 ", 234324 ," comp name , city " , 435345'.
write : / in_put.
REPLACE ALL OCCURRENCES OF ',' IN in_put WITH '#'.

WRITE: / in_put.

even though if u are not satisfied...... U need to loop at the string "in_put "...

its like...


data: b type int4.
b = strlen( a ).
do b times.

 if a+k(1) = '"'.
   x+k(1) = '!'.

so on...
enddo.

Read only

0 Likes
1,467

Thanks Kiran,

tedious but the best option is this one.



itab-rec = ' 5646," Name1, Name2 ", 234324 ," comp name , city " , 435345 '.
append itab.


loop at itab.

  length = strlen( itab-rec ).

  do length times.

    if itab-rec+i(1) = '"'.
      flag = flag + 1.
    endif.

    if itab-rec+i(1) = ','.

      flag2 = flag mod 2.

      if flag2 <> 0.
        itab-rec+i(1) = '#'.
        modify itab transporting rec.
      endif.

    endif.

    i = i + 1.

  enddo.

Read only

0 Likes
1,467

Welcome

Read only

0 Likes
1,467

Done

Read only

former_member188829
Active Contributor
0 Likes
1,467

Hi,

Try this.

DATA:in_put TYPE string,
     var1 TYPE string,
     var2 TYPE string,
     var3 TYPE string,
     var4 TYPE string,
     var5 TYPE string,
     out_put TYPE string.

DATA:len1 TYPE i.

in_put = '5646," Name1, Name2 ",234324 ," comp name , city ",435345'.

SPLIT in_put AT '"' INTO var1 var2 var3 var4 var5.

len1 = strlen( var1 ).
len1 = len1 - 1.
IF var1+len1(1) NE ',' AND var1+0(1) NE ','.
  REPLACE ALL OCCURRENCES OF ',' IN var1 WITH '#'.
ENDIF.

len1 = strlen( var2 ).
len1 = len1 - 1.
IF var2+len1(1) NE ',' AND var2+0(1) NE ','.
  REPLACE ALL OCCURRENCES OF ',' IN var2 WITH '#'.
ENDIF.

len1 = strlen( var3 ).
len1 = len1 - 1.
IF var3+len1(1) NE ',' AND var3+0(1) NE ','.
  REPLACE ALL OCCURRENCES OF ',' IN var3 WITH '#'.
ENDIF.

len1 = strlen( var4 ).
len1 = len1 - 1.
IF var4+len1(1) NE ',' AND var4+0(1) NE ','.
  REPLACE ALL OCCURRENCES OF ',' IN var4 WITH '#'.
ENDIF.

len1 = strlen( var5 ).
len1 = len1 - 1.
IF var5+len1(1) NE ',' AND var5+0(1) NE ','.
  REPLACE ALL OCCURRENCES OF ',' IN var5 WITH '#'.
ENDIF.

CONCATENATE var1 '"' var2 '"' var3 '"' var4 '"' var5 INTO out_put.

WRITE: out_put.