+ Start a Discussion
Ajay Ghuge 6Ajay Ghuge 6 

How to split string having comma inside single quote ?

Hello Everyone,

I am getting the below string format : 
string strTest = '_billing_tp_1_address_2: XYZ , _billing_tp_1_address_2: ABC,XYZ';

The code strTest.split(',') will produce 3 values : 
1) _billing_tp_1_address_2: XYZ
2)_billing_tp_1_address_2: ABC
3) XYZ

However, I want the following output: 
1) _billing_tp_1_address_2: XYZ
2)_billing_tp_1_address_2: ABC,XYZ 

I tried the different patterns of the split but they are giving the same output.

Let me know if anyone knows this split in the way I want.

Regards,
Ajay 




 
Best Answer chosen by Ajay Ghuge 6
Ajay Ghuge 6Ajay Ghuge 6
The problem solved by adding the special symbol for the second comma. While parsing check for that special symbol and replace it with a comma.

All Answers

Hemant_JainHemant_Jain
See if below code helps: 
List<String> splitStrings = combinedString.split(',');

Kindly mark it as best answer if this resolves the problem.
prashantraj bhattprashantraj bhatt
Use this-   strTest .split(',', 2). You will get your desired result
Ajay Ghuge 6Ajay Ghuge 6
Hi Prashantraj,

Thanks for your solution but it will not work in the following scenario.

 {u'_billing_tp_1_tracking_code': u'GB-SP-NA-00000000-white_belt_only', u'_billing_tp_1_postcode': u'411050', u'_billing_tp_1_product': u'1181_10253', u'_billing_tp_1_locklizard_license': u'http://www.locklizard-license2.co.uk/safeguard3/?action=getvlic&un=3ac8675bfeb98aea6b5fb18d58c1304f&pw=03363d2b71b5799e0bad058351f1e9ca', u'_billing_tp_1_country': u'IN', u'_billing_tp_1_instruction_pdf': u'https://staging.6sigma.us/wp-content/uploads/wp-designer/wc-litmos/course-instruction-pdfs/E17-White-Belt-Instructions-Litmos.pdf', u'_billing_tp_1_email': u'aniash29@gmail.com', u'_billing_tp_1_class_type': u'WB', u'_billing_tp_1_company': u'WC Test Order', u'_billing_tp_1_state': u'MH', u'_billing_tp_1_city': u'Pune', u'_billing_tp_1_address_2': u' Test,Test', u'_billing_tp_1_phone': u'9569855240', u'_billing_tp_1_last_name': u'Mach', u'_billing_tp_1_address_1': u'3, Et Amet', u'_billing_tp_1_first_name': u'Doe'}

OutPut Required : 
_billing_tp_1_tracking_code : GB-SP-NA-00000000-white_belt_only,
_billing_tp_1_address_2 : Test,Test

Regards,
Ajay
Ajay Ghuge 6Ajay Ghuge 6
The problem solved by adding the special symbol for the second comma. While parsing check for that special symbol and replace it with a comma.
This was selected as the best answer