escapeCsvCell static method

String escapeCsvCell(
  1. String value
)

Escapes a single CSV cell value.

Doubles any embedded quotes and wraps the value in quotes when it contains a comma, quote, or a newline.

Implementation

static String escapeCsvCell(String value) {
  try {
    final String string = value.replaceAll('"', '""');
    if (string.contains(',') ||
        string.contains('\n') ||
        string.contains('\r') ||
        string.contains('"')) {
      return '"$string"';
    }
    return string;
  } catch (e) {
    return value;
  }
}