Skip to content

Instantly share code, notes, and snippets.

@filiph
Last active July 11, 2021 10:22
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save filiph/d4e0c0a9efb0f869f984317372f5bee8 to your computer and use it in GitHub Desktop.
Save filiph/d4e0c0a9efb0f869f984317372f5bee8 to your computer and use it in GitHub Desktop.
A functional way to capitalize each word in a sentence (a.k.a. Title Case). This is not efficient -- use something like string_scanner if you need to run this in a tight loop.
main() {
var city = "new york";
print(titleCase(city));
}
/// Inefficient way of capitalizing each word in a string.
String titleCase(String text) {
if (text.length <= 1) return text.toUpperCase();
var words = text.split(' ');
var capitalized = words.map((word) {
var first = word.substring(0, 1).toUpperCase();
var rest = word.substring(1);
return '$first$rest';
});
return capitalized.join(' ');
}
@jirihradil
Copy link

OK, what is an efficient way?

@JosLuna98
Copy link

main() {
  String spanish = "esto es una prueba con varias palabras";

  /// First option.
  print(titleCase(spanish));

  /// Second option.
  String titleCaseVar = spanish
      .split(' ')
      .map((word) => word[0].toUpperCase() + word.substring(1))
      .join(' ');
  print(titleCaseVar);
}

/// My way of capitalizing each word in a string.
String titleCase(String text) {
  if (text == null) throw ArgumentError("string: $text");

  if (text.isEmpty) return text;

  /// If you are careful you could use only this part of the code as shown in the second option.
  return text
      .split(' ')
      .map((word) => word[0].toUpperCase() + word.substring(1))
      .join(' ');
}

@encikpulasan
Copy link

encikpulasan commented Aug 20, 2020

I've converted to String extension as below:

extension CapExtension on String {
  //first letter only
  String get inCaps => '${this[0].toUpperCase()}${this.substring(1)}';
  //all letter in string
  String get allInCaps => this.toUpperCase();
  //first letter for each word in a string
  String get titleCase => this
      .split(' ')
      .map((word) => word[0].toUpperCase() + word.substring(1))
      .join(' ');
}

Hope it helps. :)

@hacker1024
Copy link

hacker1024 commented Oct 12, 2020

Here's my implementation, using a StringBuffer and UTF-16 values (this capitalizes letters after periods as well):

  /// Fixes name cases; Capitalizes Each Word.
  String normaliseName(String name) {
    final stringBuffer = StringBuffer();

    var capitalizeNext = true;
    for (final letter in name.toLowerCase().codeUnits) {
      // UTF-16: A-Z => 65-90, a-z => 97-122.
      if (capitalizeNext && letter >= 97 && letter <= 122) {
        stringBuffer.writeCharCode(letter - 32);
        capitalizeNext = false;
      } else {
        // UTF-16: 32 == space, 46 == period
        if (letter == 32 || letter == 46) capitalizeNext = true;
        stringBuffer.writeCharCode(letter);
      }
    }

    return stringBuffer.toString();
  }

@hesbonomanjo
Copy link

Here's my implementation, using a StringBuffer and UTF-16 values (this capitalizes letters after periods as well):

  /// Fixes name cases; Capitalizes Each Word.
  String normaliseName(String name) {
    final stringBuffer = StringBuffer();

    var capitalizeNext = true;
    for (final letter in name.toLowerCase().codeUnits) {
      // UTF-16: A-Z => 65-90, a-z => 97-122.
      if (capitalizeNext && letter >= 97 && letter <= 122) {
        stringBuffer.writeCharCode(letter - 32);
        capitalizeNext = false;
      } else {
        // UTF-16: 32 == space, 46 == period
        if (letter == 32 || letter == 46) capitalizeNext = true;
        stringBuffer.writeCharCode(letter);
      }
    }

    return stringBuffer.toString();
  }

Thank you so much! @hacker1024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment