removeSurroundingSpecialCharacters function

String removeSurroundingSpecialCharacters (
  1. String string
)

Returns string without trailing and leading special characters.

Implementation

String removeSurroundingSpecialCharacters(String string) {
  var specialChar = RegExp(r'[^a-zA-Z0-9\s]*');
  string = string.replaceFirst(specialChar, '');
  Iterable<Match> matches = RegExp(r'[a-zA-Z0-9\s]').allMatches(string);
  if (matches.length > 0) {
    string = string.replaceFirst(specialChar, '', matches.last.start + 1);
  }
  return string;
}