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

regarding string operations

Former Member
0 Likes
1,138

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

1 ACCEPTED SOLUTION
Read only

christian_wohlfahrt
Active Contributor
0 Likes
1,094

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

9 REPLIES 9
Read only

christian_wohlfahrt
Active Contributor
0 Likes
1,095

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

Read only

RichHeilman
Developer Advocate
Developer Advocate
0 Likes
1,094

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

Read only

Former Member
0 Likes
1,094

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.

Read only

0 Likes
1,094

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 ?

Read only

0 Likes
1,094

you can use replace all occurences of ','.

regards

vijay

Read only

0 Likes
1,094

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

Read only

Former Member
0 Likes
1,094

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

Read only

Former Member
0 Likes
1,094

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

Read only

Former Member
0 Likes
1,094

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