Application Development 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: 

FM to not split string containing delimiter

vg24
Explorer

I have a csv file which contains values separated by semi-colon(;) but there is also a string containing the semi-colon. I want to separate the values based on semi-colon except the one within quotes (i.e. the string).

Is there any function module of method in SAP ABAP using which the string containing delimiter is not separated but the rest of the values are?

15 REPLIES 15

FredericGirod
Active Contributor

You could do a simple split, and re-read all value & check if a value start with " it should end with " or you have to find the next row ending with a " (and not starting)

vg24
Explorer
0 Kudos

frdric.girod this can be done but I want a FM which can do this. Is there any FM already present which can do this?

FredericGirod
Active Contributor
0 Kudos

1. FM is not the Good Way of Working, FM is old school and using FM is taking a risk for futur upgrade

2. There is not a Golden List of nice FM doing whatever you want

3. Code in Object !!!

prabukannans
Participant
0 Kudos

Hi Vansika,

Instead of using the Function module, you can directly use the split statement in abap.

data : lv_module type string value 'sd;mm;fi;co'.
data : lv_tech1 type string,
lv_tech2 type string,
lv_tech3 type string,
lv_tech4 type string.

split lv_module at ';' INTO lv_tech1
lv_tech2
lv_tech3
lv_tech4.

write : lv_tech1,
/ lv_tech2,
/ lv_tech3,
/ lv_tech4.

That would be nice from you if you format your ABAP code with colors, indents and so on: that is automatically done by editing your question, selecting the lines and pressing the button CODE.

abo
Active Contributor

also, this probably doesn't handle the case above, with semi-colon appearing in quotes.

vg24
Explorer

Yes. but this is a customer requirement.

Also, I found a FM after researching. Thanks for the help though 🙂

FredericGirod
Active Contributor
0 Kudos

I could understand if it is a final customer requirement 😞

Could you post your FM for futur Customer strange question

Rashid_Javed
Contributor
0 Kudos

Interesting. I would suggest to lets look at the problem before looking for a method or function. As you mentioned it is a CSV file that is using ; as a separator but also there is a string field in it that can also contain a ; in it (part of string text). so i assume the file structure could be like following

Field1;Field2;"StringFieldWith;and text"

In this case, you should split first at " (quote) so that you string field is separate from the other fields and then you can split at ; (semi colon). Like for the above structure we should have something like following code. Please note i just types it. may contain syntax errors but you should get the idea what i am suggesting.

* Suppose file structure is Field1;Field2;"StringFieldWith;and text" 
DataRow = Field1;Field2;"StringFieldWith;and text"
 
Data: lv_buff1 type string, lv_buff2 type string.
Data begin of ls_file,
          field1 type string,
          field2 type string,
          field3 type string,
     end of ls_file.
split DataRow at '"' into lv_buff1 lv_buff2.
* Now string field should be in lv_buff2 and other fields in lv_buff1 
* so you can split lv_buff1 at ; to get the rest of the fields
if sy-subrc = 0.
split lv_buf1 at ';' into ls_file-field1 ls_file-field2.
ls_file-field3 = lv_buff2.
endif.
<br>
RJv

ThorstenHoefer
Active Contributor

Hi vg24,

do you have the possibility to save the excel file as text file tab delimited?

In this case, you can split the line by cl_abap_char_utilities=>HORIZONTAL_TAB

Best Regards
Thorsten

Sandra_Rossi
Active Contributor
0 Kudos

It's always good to search before posting 🙂

Subhankar
Active Contributor

Please check this standard method cl_rsda_csv_converter=>csv_to_structure. This is for comma delimited. You can use the same logic.

DATA: lr_csv TYPE REF TO cl_rsda_csv_converter,
ls_file TYPE ty_read_file,
lv_string TYPE string.


CALL METHOD cl_rsda_csv_converter=>create
RECEIVING
r_r_conv = lr_csv.

DELETE ip_t_string INDEX 1.

LOOP AT ip_t_string INTO lv_string.
TRY.
lr_csv->csv_to_structure(
EXPORTING
i_data = lv_string
IMPORTING
e_s_data = ls_file ). --> Here you will get in your structure

Endloop.

0 Kudos

Standard doesn't mean "released" (released = if there's a bug, SAP will support you and fix it if your point is valid).

raymond_giuseppi
Active Contributor
0 Kudos

TEXT_CONVERT_CSV_TO_SAP ?

0 Kudos

Hi Giuseppi,

I just tested FM TEXT_CONVERT_CSV_TO_SAP. It doesn't respect the quotes. Can't help in this case.

Regards,

Nemanja