‎2006 Jan 19 4:05 PM
Hi All,
i've a string holding the value as given below.
AA,17,2/19/2003,"9,999.00",USD,00,10,318,"193,275.31"
by performing some string operations i want the result string in the format as given below:
AA,17,2/19/2003,"9999.00",USD,00,10,318,"193,275.31"
i.e., i want to remove all the commas(,)that are included in between a pair of " " only.
can anyone provide me a sample code for the same
‎2006 Jan 19 4:25 PM
Hi Vijay!
Maybe you have to do it the hard way:
- Move the input-line to a char field (l_text type char255)
- start a do-loop.
- assign one char after each other to a field symbol
- check, if content is equal ": if yes, switch a flag (on or off)
- check, if content is equal ',' and flag is on: replace content with space (because of assign, you change l_text)
- enddo.
- condense l_text -> all space are gone
Here the help for assign:
DATA text(10) TYPE c VALUE '0123456789'.
FIELD-SYMBOLS <char> TYPE c.
DATA off TYPE i.
DO.
off = sy-index - 1.
ASSIGN text+off(1) TO <char>.
IF sy-subrc ne 0.
EXIT.
ENDIF.
WRITE / <char>.
ENDDO.
Regards,
Christian
‎2006 Jan 19 4:25 PM
Hi Vijay!
Maybe you have to do it the hard way:
- Move the input-line to a char field (l_text type char255)
- start a do-loop.
- assign one char after each other to a field symbol
- check, if content is equal ": if yes, switch a flag (on or off)
- check, if content is equal ',' and flag is on: replace content with space (because of assign, you change l_text)
- enddo.
- condense l_text -> all space are gone
Here the help for assign:
DATA text(10) TYPE c VALUE '0123456789'.
FIELD-SYMBOLS <char> TYPE c.
DATA off TYPE i.
DO.
off = sy-index - 1.
ASSIGN text+off(1) TO <char>.
IF sy-subrc ne 0.
EXIT.
ENDIF.
WRITE / <char>.
ENDDO.
Regards,
Christian
‎2006 Jan 19 4:34 PM
Vijay, this may not look pretty but it does work. Check it out.
report zrich_0003 .
data: pos1 type i,
pos2 type i,
diff type i.
data: string type string.
data: c(1000) type c.
string = 'AA,17,2/19/2003,"9,999.00",USD,00,10,318,"193,275.31"'.
c = string.
while sy-subrc = 0.
search c for '"'.
if sy-subrc = 0.
pos1 = sy-fdpos + 2.
search c for '"' starting at pos1.
if sy-subrc = 0.
pos2 = sy-fdpos + 2.
pos1 = pos1 - 2.
while sy-subrc = 0.
replace ',' with space into c+pos1(pos2).
endwhile.
sy-subrc = 0.
while sy-subrc = 0.
replace '"' with space into c+pos1(pos2).
endwhile.
condense c+pos1(pos2) no-gaps.
sy-subrc = 0.
endif.
endif.
endwhile.
condense c no-gaps.
write:/ c.
Regards,
Rich Heilman
‎2006 Jan 19 4:39 PM
Hi Vijay,
Try this code.
data: text(255).
data: v_x.
text = 'AA,17,2/19/2003,"9,999.00",USD,00,10,318,"193,275.31"'.
do.
v_x = ' '.
search text for '"' and mark.
if sy-subrc = 0.
replace first OCCURRENCE OF '"' in section offset sy-fdpos of text with
''.
replace first OCCURRENCE OF ',' in section offset sy-fdpos of text with
''.
replace first OCCURRENCE OF '",' in section offset sy-fdpos of text with
','.
v_x = 'X'.
endif.
if v_x = ' '.
exit.
endif.
enddo.
write:/ text.
‎2006 Jan 20 6:42 AM
Hi Phani,
ur logic worked great, but if i've have more than one comma in between a pair of " " it replaces only the first one. how can i handle this scenario ?
‎2006 Jan 20 6:56 AM
‎2006 Jan 20 7:20 AM
Dear Vijay ,
Please try this code. Hope this would give the required result.
Report rpt .
Data: strtest type c length 100.
data: i type i.
data: j type i.
data: k type c. " Indicator flag for "
strtest = 'AA,17,2/19/2003,"9,999.00",USD,00,10,318,"193,275.31"'.
message strtest type 'I'.
i = 0.
j = strlen( strtest ).
while i < j.
if strtest+i(1) = '"' and k = ''.
k = 'X'.
elseif strtest+i(1) = '"' and k = 'X'.
k = space.
else.
endif.
if strtest+i(1) = ',' and k = 'X'.
strtest+i(1) = ''.
endif.
i = i + 1.
endwhile.
message strtest type 'I'.
Regards,
Shankar
‎2006 Jan 19 4:48 PM
Hi vijay,
A bit complex but works for sure, check the following logic,
REPORT zsritest.
DATA: gs_string TYPE string.
gs_string = 'AA,17,2/19/2003,"9,999.00",USD,00,10,318,"193,275.31"'.
WRITE: / gs_string.
PERFORM string_trim CHANGING gs_string.
*---------------------------------------------------------------------*
* FORM string_trim *
*---------------------------------------------------------------------*
* ........ *
*---------------------------------------------------------------------*
* --> LS_STRING *
*---------------------------------------------------------------------*
FORM string_trim CHANGING ls_string.
DATA: lt_string TYPE string OCCURS 0 WITH HEADER LINE,
lv_tabix TYPE i,
lv_start.
SPLIT gs_string AT '"' INTO TABLE lt_string.
CHECK sy-subrc EQ 0.
CLEAR gs_string.
LOOP AT lt_string.
lv_tabix = sy-tabix MOD 2.
IF lv_tabix EQ 0.
TRANSLATE lt_string USING ', '.
CONDENSE lt_string NO-GAPS.
ENDIF.
IF lv_tabix EQ 0 OR lv_start EQ 'X'.
CONCATENATE gs_string lt_string INTO gs_string SEPARATED BY '"'.
IF lv_start EQ 'X'.
CLEAR lv_start.
ELSE.
lv_start = 'X'.
ENDIF.
ELSE.
CONCATENATE gs_string lt_string INTO gs_string.
ENDIF.
ENDLOOP.
IF lv_start EQ 'X'.
CONCATENATE gs_string '"' INTO gs_string.
ENDIF.
WRITE: / gs_string.
ENDFORM.Hope this helps..
Sri
‎2006 Jan 20 9:02 AM
Hi vijay,
As you know that if you split this string with delimiter as ,then you will get 11 strings. right.
In that these strings you donot need , between 4 and 5
& 10 and 11. Right.
Use the bellow code.
data str1(20),
str2(20),
str3(20),
str4(20),
str5(20),
str6(20),
str7(20),
str8(20),
str9(20),
str10(20),
str11(20).
SPLIT source_string AT ',' INTO
str1,str2,str3,str4,str5,str6,str7,str8,str9,str10,str11.
concatenate str1, ',',
str2, ',',
str3, ',',
str4,
str5, ',',
str6, ',',
str7, ',',
str8, ',',
str9, ',',
str10,
str11.
hope it helps.
Regards,
Maheswaran.B
Message was edited by: Maheswaran B
‎2006 Jan 20 10:18 AM
Hi Vijay,
u can do this using offset functionality.read each letter from the string when ever u encounter ".pass the letters after that inverted comma (") until another one is encountered in between this if u encounter a comma delete it.and finally concatenate that string
Regards,
Suresh
Message was edited by: suresh B