Skip to main content

avoid-substring

added in: 1.6.0
Free+

Warns when a String.substring method is used.

String.substring does not support UTF-16 which can result in unexpected output when a string contains emojis.

Use package:characters and characters.getRange instead.

Example

❌ Bad:

String someValue = '🙂';

// LINT: Avoid calling '.substring' as it does not support UTF-16 (ex. emojis).
// This may lead to unexpected results. Try using 'characters.getRange' instead.
someValue.substring(0, 1);

✅ Good:

String someValue = '🙂';

someValue.characters.getRange(0, 1);

Additional Resources