toSnakeCase static method

String toSnakeCase(
  1. String s
)

Converts a string to snake case.

The method takes a string, removes any non-alphanumeric characters, splits the string into words, and then joins them back together with an underscore (_) character between each word. The entire string is then converted to lowercase.

Implementation

static String toSnakeCase(String s) {
  // Remove any non-alphanumeric characters and split the string into words
  List<String> words =
      s.replaceAll(RegExp(r'[^A-Za-z0-9\s]'), '').trim().split(' ');
  // Join the words back together with an underscore (_) character between each word
  // and convert the entire string to lowercase
  return words.join('_').toLowerCase();
}