diff --git a/theodolite/src/main/kotlin/rocks/theodolite/core/IOHandler.kt b/theodolite/src/main/kotlin/rocks/theodolite/core/IOHandler.kt index 78b353a1e525984575e63830ee16b5c60f02c3ae..e70779afb34c4e7cecd561a2c4d64b09c9933ad5 100644 --- a/theodolite/src/main/kotlin/rocks/theodolite/core/IOHandler.kt +++ b/theodolite/src/main/kotlin/rocks/theodolite/core/IOHandler.kt @@ -85,13 +85,20 @@ class IOHandler { /** * For a list of Strings: - * Adds quotation marks around strings that contain a comma + * - adds additional quotation mark to existing one + * - adds quotation marks around entries that contain a comma */ private fun addQuotationMarks(stringList: List<String> ): List<String> { val stringMutableList = stringList.toMutableList() stringMutableList.forEachIndexed { index, entry -> + // add additional quotation marks to escape them in csv + if (entry.contains("\"")){ + stringMutableList[index] = stringMutableList[index].replace('"'+"", "\"" + '"') + } + + // add quotation marks around entries that contain a comma if (entry.contains(",")){ - stringMutableList[index] = "\"" + entry + "\"" + stringMutableList[index] = '"' + stringMutableList[index] + '"' } } return stringMutableList diff --git a/theodolite/src/test/kotlin/rocks/theodolite/core/IOHandlerTest.kt b/theodolite/src/test/kotlin/rocks/theodolite/core/IOHandlerTest.kt index 938eeaa7a3c8f14bdebf8d3d341bc5b209e3f44d..e54bf7851b195927f69666caa2fd3b33fb3673d7 100644 --- a/theodolite/src/test/kotlin/rocks/theodolite/core/IOHandlerTest.kt +++ b/theodolite/src/test/kotlin/rocks/theodolite/core/IOHandlerTest.kt @@ -68,7 +68,7 @@ internal class IOHandlerTest { } /** - * Tests if values with commas are surrounded with quotation marks. + * Tests if values with commas and quotation marks are surrounded with additional quotation marks. */ @Test fun testWriteToCSVFileWithComma() { @@ -79,13 +79,13 @@ internal class IOHandlerTest { val expectedColumns = listOf("\"Fruit, Fruit2\"", "Color") val testContent = listOf( - listOf("apples, paprika", "red"), + listOf("apples, "+ '"' + "paprika" + '"', "red"), listOf("bananas, pineapple", "yellow"), listOf("avocado, coconut", "brown") ) val expectedContent = listOf( - listOf("\"apples, paprika\"", "red"), + listOf("\"apples, " + '"' + '"' + "paprika" + '"' + '"' + '"', "red"), listOf("\"bananas, pineapple\"", "yellow"), listOf("\"avocado, coconut\"", "brown") )