Zeba Academy String Utils

pub package License: GPL v3 Dart Flutter

A lightweight, dependency-free collection of string manipulation utilities for Dart and Flutter applications.

zeba_academy_string_utils provides practical helpers for whitespace cleanup, truncation, repetition, character validation, word counting, safe substring operations, normalization, and more.

โœจ Features

  • ๐Ÿงน Trim leading and trailing whitespace
  • ๐Ÿงฝ Clean and collapse consecutive whitespace
  • ๐Ÿšซ Remove all whitespace
  • โœ‚๏ธ Safely truncate strings
  • ๐Ÿ” Repeat strings
  • ๐Ÿ”ค Check alphabetic strings
  • ๐Ÿ”ข Check numeric strings
  • ๐Ÿ”ค๐Ÿ”ข Check alphanumeric strings
  • ๐Ÿ”  Detect uppercase characters
  • ๐Ÿ”ก Detect lowercase characters
  • ๐Ÿ”ข Detect digits
  • โ  Detect whitespace
  • ๐Ÿ“ Count words
  • ๐Ÿ›ก๏ธ Safely extract substrings
  • ๐Ÿงน Normalize strings
  • ๐Ÿ”ก Convert strings to lowercase
  • ๐Ÿ”  Convert strings to uppercase
  • โฌœ Detect blank strings
  • ๐Ÿงฉ Convenient String extensions
  • ๐Ÿ“ฆ Zero external runtime dependencies
  • ๐ŸŽฏ Simple and predictable API

๐Ÿ“ฆ Installation

Add the package to your pubspec.yaml:

dependencies:
  zeba_academy_string_utils: ^1.0.0

Then run:

flutter pub get

๐Ÿš€ Quick Start

Import the package:

import 'package:zeba_academy_string_utils/zeba_academy_string_utils.dart';

You can use the static utility API:

final cleaned = ZebaStringUtils.cleanWhitespace(
  '  Hello    Flutter   World  ',
);

print(cleaned);
// Hello Flutter World

Or use the convenient string extensions:

final result = '  HELLO   WORLD  '.zNormalize();

print(result);
// hello world

๐Ÿงฐ API Reference

Whitespace Utilities

trim

Removes leading and trailing whitespace.

final result = ZebaStringUtils.trim('  Hello World  ');

print(result);
// Hello World

Extension:

final result = '  Hello World  '.zTrim();

cleanWhitespace

Trims the string and collapses consecutive whitespace characters into a single space.

final result = ZebaStringUtils.cleanWhitespace(
  '  Hello    beautiful   world  ',
);

print(result);
// Hello beautiful world

Extension:

final result = '  Hello    World  '.zCleanWhitespace();

removeWhitespace

Removes all whitespace characters.

final result = ZebaStringUtils.removeWhitespace(
  'Hello World',
);

print(result);
// HelloWorld

Extension:

final result = 'Hello World'.zRemoveWhitespace();

โœ‚๏ธ Truncation

truncate

Truncates a string to the specified maximum length.

final result = ZebaStringUtils.truncate(
  'Hello Flutter Developer',
  12,
);

print(result);
// Hello Fl...

The default ellipsis is:

...

You can provide a custom ellipsis:

final result = ZebaStringUtils.truncate(
  'Hello Flutter Developer',
  12,
  ellipsis: 'โ€ฆ',
);

print(result);
// Hello Fluttโ€ฆ

Extension:

final result = 'Hello Flutter Developer'.zTruncate(12);

Edge Cases

If the string is already within the requested length, it is returned unchanged.

ZebaStringUtils.truncate('Hello', 10);
// Hello

A negative maxLength throws an ArgumentError.


๐Ÿ” Repetition

repeat

Repeats a string the requested number of times.

final result = ZebaStringUtils.repeat(
  'ab',
  3,
);

print(result);
// ababab

Extension:

final result = 'ab'.zRepeat(3);

A count of zero returns an empty string:

ZebaStringUtils.repeat('Hello', 0);
// ''

A negative count throws an ArgumentError.


๐Ÿ”ค Character Checks

isAlphabetic

Checks whether a string contains only alphabetic ASCII characters.

ZebaStringUtils.isAlphabetic('Hello');
// true

ZebaStringUtils.isAlphabetic('Hello123');
// false

Extension:

'Hello'.zIsAlphabetic;

isNumeric

Checks whether a string contains only numeric ASCII digits.

ZebaStringUtils.isNumeric('12345');
// true

ZebaStringUtils.isNumeric('123abc');
// false

Extension:

'12345'.zIsNumeric;

isAlphanumeric

Checks whether a string contains only alphabetic and numeric ASCII characters.

ZebaStringUtils.isAlphanumeric('Flutter123');
// true

ZebaStringUtils.isAlphanumeric('Flutter 123');
// false

Extension:

'Flutter123'.zIsAlphanumeric;

hasUppercase

Checks whether the string contains at least one uppercase character.

ZebaStringUtils.hasUppercase('helloWorld');
// true

ZebaStringUtils.hasUppercase('hello');
// false

Extension:

'helloWorld'.zHasUppercase;

hasLowercase

Checks whether the string contains at least one lowercase character.

ZebaStringUtils.hasLowercase('HELLOw');
// true

ZebaStringUtils.hasLowercase('HELLO');
// false

Extension:

'HELLOw'.zHasLowercase;

hasDigit

Checks whether the string contains at least one digit.

ZebaStringUtils.hasDigit('Flutter123');
// true

ZebaStringUtils.hasDigit('Flutter');
// false

Extension:

'Flutter123'.zHasDigit;

hasWhitespace

Checks whether the string contains at least one whitespace character.

ZebaStringUtils.hasWhitespace('Hello World');
// true

ZebaStringUtils.hasWhitespace('Hello');
// false

Extension:

'Hello World'.zHasWhitespace;

๐Ÿ“ Word Counting

wordCount

Counts words separated by whitespace.

final count = ZebaStringUtils.wordCount(
  'Hello beautiful Flutter world',
);

print(count);
// 4

Multiple whitespace characters are handled automatically:

ZebaStringUtils.wordCount(
  'Hello    beautiful\nFlutter\tworld',
);
// 4

Empty or whitespace-only strings return 0.

Extension:

final count = 'Hello Flutter Developer'.zWordCount;

๐Ÿ›ก๏ธ Safe Substring

safeSubstring

Extracts a substring without throwing when the requested indexes exceed the string's boundaries.

final result = ZebaStringUtils.safeSubstring(
  'Hello World',
  0,
  100,
);

print(result);
// Hello World

A negative start index is safely clamped:

ZebaStringUtils.safeSubstring(
  'Hello',
  -10,
  3,
);
// Hel

An index beyond the string length returns an empty string:

ZebaStringUtils.safeSubstring(
  'Hello',
  100,
);
// ''

You can also omit the end index:

ZebaStringUtils.safeSubstring(
  'Hello World',
  6,
);
// World

Extension:

final result = 'Hello World'.zSafeSubstring(0, 100);

๐Ÿงน Normalization

normalize

Normalizes a string by:

  1. Trimming leading whitespace.
  2. Trimming trailing whitespace.
  3. Collapsing consecutive whitespace.
  4. Converting the result to lowercase.
final result = ZebaStringUtils.normalize(
  '  HELLO    Flutter   WORLD  ',
);

print(result);
// hello flutter world

Extension:

final result = '  HELLO   WORLD  '.zNormalize();

This is useful when comparing user input:

final first = ZebaStringUtils.normalize('  Hello World ');
final second = ZebaStringUtils.normalize('HELLO   WORLD');

print(first == second);
// true

๐Ÿ”ก Case Conversion

lowercase

final result = ZebaStringUtils.lowercase('HELLO');

print(result);
// hello

Extension:

'HELLO'.zLowercase();

uppercase

final result = ZebaStringUtils.uppercase('hello');

print(result);
// HELLO

Extension:

'hello'.zUppercase();

โฌœ Blank String Checks

isBlank

Checks whether a string is empty after trimming whitespace.

ZebaStringUtils.isBlank('');
// true

ZebaStringUtils.isBlank('   ');
// true

ZebaStringUtils.isBlank('Hello');
// false

Extension:

'   '.zIsBlank;

isNotBlank

The opposite of isBlank.

ZebaStringUtils.isNotBlank('Hello');
// true

ZebaStringUtils.isNotBlank('   ');
// false

Extension:

'Hello'.zIsNotBlank;

๐Ÿงฉ Extension API

The package provides convenient extensions directly on String.

import 'package:zeba_academy_string_utils/zeba_academy_string_utils.dart';

void main() {
  final value = '  Hello   Flutter 123  ';

  print(value.zTrim());
  print(value.zCleanWhitespace());
  print(value.zRemoveWhitespace());
  print(value.zTruncate(10));
  print(value.zRepeat(2));

  print(value.zIsAlphabetic);
  print(value.zIsNumeric);
  print(value.zIsAlphanumeric);

  print(value.zHasUppercase);
  print(value.zHasLowercase);
  print(value.zHasDigit);
  print(value.zHasWhitespace);

  print(value.zWordCount);
  print(value.zSafeSubstring(0, 100));
  print(value.zNormalize());

  print(value.zLowercase());
  print(value.zUppercase());

  print(value.zIsBlank);
  print(value.zIsNotBlank);
}

๐ŸŽฏ Static API vs Extensions

Both styles are supported.

Static API

Useful when you want an explicit utility class:

ZebaStringUtils.normalize(value);

Extension API

Useful when working directly with strings:

value.zNormalize();

Choose whichever style fits your project.


๐Ÿ“‹ Complete API

Method Description
trim() Removes surrounding whitespace
cleanWhitespace() Trims and collapses whitespace
removeWhitespace() Removes all whitespace
truncate() Safely truncates a string
repeat() Repeats a string
isAlphabetic() Checks alphabetic characters
isNumeric() Checks numeric characters
isAlphanumeric() Checks alphanumeric characters
hasUppercase() Checks for uppercase characters
hasLowercase() Checks for lowercase characters
hasDigit() Checks for digits
hasWhitespace() Checks for whitespace
wordCount() Counts words
safeSubstring() Safely extracts a substring
normalize() Normalizes whitespace and case
lowercase() Converts to lowercase
uppercase() Converts to uppercase
isBlank() Checks for blank strings
isNotBlank() Checks for non-blank strings

๐Ÿงช Testing

The package includes comprehensive tests covering:

  • Whitespace handling
  • Truncation
  • Custom ellipsis
  • String repetition
  • Character validation
  • Uppercase/lowercase detection
  • Digit detection
  • Word counting
  • Safe substring boundaries
  • Normalization
  • Case conversion
  • Blank string detection
  • Extension methods
  • Edge cases
  • Invalid arguments

Run tests with:

flutter test

Run static analysis with:

flutter analyze

Format the project with:

dart format lib test

๐Ÿ”’ Dependencies

zeba_academy_string_utils has zero external runtime dependencies.

It uses only the Dart core libraries and is designed to remain lightweight and easy to integrate into existing Dart and Flutter projects.


๐ŸŽฏ Design Goals

This package follows a few simple principles:

  • Simple โ€” APIs should be easy to understand.
  • Predictable โ€” Edge cases should have consistent behavior.
  • Lightweight โ€” No unnecessary dependencies.
  • Reusable โ€” Utilities should work across Dart and Flutter applications.
  • Type-safe โ€” APIs should leverage Dart's type system.
  • Tested โ€” Common and edge-case behavior should be covered by tests.
  • Production-ready โ€” Suitable for real-world applications.

๐Ÿค Contributing

Contributions are welcome!

If you discover a bug, have an improvement, or want to suggest a feature:

  1. Fork the repository.
  2. Create a feature branch.
  3. Make your changes.
  4. Add or update tests.
  5. Run dart format.
  6. Run flutter analyze.
  7. Run flutter test.
  8. Submit a pull request.

Please keep contributions focused and aligned with the package's goal of providing small, reusable string utilities.


๐Ÿ“„ License

This project is licensed under the GNU General Public License v3.0 (GPL-3.0).

See the LICENSE file for the complete license text.


๐Ÿ‘จโ€๐Ÿ’ป About Me

โœจ I'm Sufyan bin Uzayr, an open-source developer passionate about building and sharing meaningful projects.

You can learn more about me and my work at sufyanism.com or connect with me on LinkedIn.


๐ŸŽ“ Your All-in-One Learning Hub!

๐Ÿš€ Explore courses and resources in coding, tech, and development at zeba.academy and code.zeba.academy.

Empower yourself with practical skills through curated tutorials, real-world projects, and hands-on experience. Level up your tech game today! ๐Ÿ’ปโœจ

Zeba Academy is a learning platform dedicated to coding, technology, and development.

โžก Visit our main site: zeba.academy

โžก Explore hands-on courses and resources at code.zeba.academy

โžก Check out our YouTube for more tutorials: Zeba Academy

โžก Follow us on Instagram: Zeba Academy

Thank you for visiting! โค๏ธ


โญ Support

If you find this package useful, consider giving the project a โญ on GitHub and sharing it with other Dart and Flutter developers.

Built with โค๏ธ for the Dart & Flutter community by Zeba Academy.