replaceQuote method

String replaceQuote(
  1. String original, [
  2. int desiredCharacter = CHAR_SINGLE_QUOTE
])
inherited

Takes a quoted or unquoted string and replaces the quotes with the given desired rune.

Only replaces quotes in the first and last positions of the string

Implementation

String replaceQuote(
  String original, [
  int desiredCharacter = CHAR_SINGLE_QUOTE,
]) {
  var firstCharacter = 0;
  var index = 0;
  final buffer = StringBuffer();
  for (final character in original.runes) {
    if (index == 0) {
      buffer.writeCharCode(desiredCharacter);
      if (StringHelper.isQuotes(character)) {
        firstCharacter = character;
      } else {
        buffer.writeCharCode(character);
      }
    } else if (index == original.length - 1) {
      if (character != firstCharacter) {
        buffer.writeCharCode(character);
      }
      buffer.writeCharCode(desiredCharacter);
    } else {
      buffer.writeCharCode(character);
    }
    index += 1;
  }
  return buffer.toString();
}