toCamelCase static method

String toCamelCase(
  1. String input
)

Converts a string to camel case.

The method takes a string, removes any non-alphanumeric characters, splits the string into words, and then joins them back together with the first letter of each word capitalized, except the first word which is entirely lowercased.

Implementation

static String toCamelCase(String input) {
  // Remove any non-alphanumeric characters and split the string into words
  final words =
      input.replaceAll(RegExp(r'[^A-Za-z0-9\s]'), '').trim().split(' ');

  var result = '';
  for (var i = 0; i < words.length; i++) {
    final word = words[i];
    if (i > 0) {
      // Capitalize the first letter of each word except the first one
      result += '${word[0].toUpperCase()}${word.substring(1)}';
    } else {
      // Lowercase the first word
      result += word.toLowerCase();
    }
  }

  return result;
}