Str class

Static utility methods for string manipulation.

Str.slug('Hello World');         // 'hello-world'
Str.camel('foo_bar');             // 'fooBar'
Str.limit('A long sentence', 6);  // 'A long...'
Str.uuid();                       // '6ba7b810-9dad-4...'

Available helpers:

Search / position:

Case conversions:

Trimming / capping:

Replace / remove / transform:

Slug:

Padding:

Substring / chars:

Mask:

Random / IDs:

Validation:

Properties

hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited

Static Methods

after(String subject, String search) String
Returns the portion of subject after the first occurrence of search. Returns subject unchanged if search is empty or not found.
afterLast(String subject, String search) String
Returns the portion of subject after the last occurrence of search.
before(String subject, String search) String
Returns the portion of subject before the first occurrence of search.
beforeLast(String subject, String search) String
Returns the portion of subject before the last occurrence of search.
between(String subject, String from, String to) String
Returns the portion of subject between from and to.
betweenFirst(String subject, String from, String to) String
Returns the smallest portion of subject between from and to.
camel(String value) String
Converts value to camelCase.
charAt(String subject, int index) String?
Returns the character at index (negative indexes count from the end).
contains(String haystack, dynamic needles, {bool ignoreCase = false}) bool
Determines if haystack contains any of the needles.
containsAll(String haystack, List needles, {bool ignoreCase = false}) bool
Determines if haystack contains all the needles.
deduplicate(String value, [String character = ' ']) String
Replaces consecutive runs of character with a single instance.
endsWith(String haystack, dynamic needles) bool
Determines if haystack ends with any of the needles.
excerpt(String text, String phrase, {int radius = 100, String omission = '...'}) String?
Extracts a snippet of text around the first occurrence of phrase.
finish(String value, String cap) String
Caps value with a single instance of cap.
headline(String value) String
Converts value to a "Headline" — words split, then title cased.
is_(dynamic pattern, String value) bool
Determines if value matches the given pattern. Asterisks are wildcards.
isAscii(String value) bool
Returns true if value is 7-bit ASCII.
isJson(String value) bool
Returns true if value is valid JSON.
isUlid(String value) bool
Returns true if value is a ULID (26 chars, Crockford base32).
isUrl(String value) bool
Returns true if value is a valid URL.
isUuid(String value) bool
Returns true if value is a valid UUID (any version).
kebab(String value) String
Converts value to kebab-case.
lcfirst(String value) String
Lowercases the first character of value.
length(String value) int
Returns the length of value.
limit(String value, [int limit = 100, String end = '...']) String
Truncates value to limit characters and appends end.
lower(String value) String
Lowercases value.
mask(String string, String character, int index, [int? length]) String
Masks a portion of string with character, starting at index for length.
match(Pattern pattern, String subject) String?
Returns the first regex match of pattern in subject, or null.
matchAll(Pattern pattern, String subject) List<String>
Returns all regex matches of pattern in subject.
padBoth(String value, int length, [String pad = ' ']) String
Pads both sides of value with pad until length is reached.
padLeft(String value, int length, [String pad = ' ']) String
Pads the left side of value.
padNumber(String value, int length) String
Pads value with leading zeros to reach length characters.
padRight(String value, int length, [String pad = ' ']) String
Pads the right side of value.
password(int length, {bool letters = true, bool numbers = true, bool symbols = true, bool spaces = false}) String
Generates a cryptographically random password.
position(String haystack, String needle, {int offset = 0}) int?
Returns the index of the first occurrence of needle in haystack, or null if not found. offset starts the search from a position.
random([int length = 16]) String
Generates a cryptographically random alphanumeric string of length.
remove(dynamic search, String subject, {bool caseSensitive = true}) String
Removes any of search from subject.
repeat(String value, int times) String
Repeats value times times.
replace(dynamic search, dynamic replace, String subject) String
Replaces every occurrence of search with replace in subject.
replaceFirst(String search, String replace, String subject) String
Replaces the first occurrence of search with replace in subject.
replaceLast(String search, String replace, String subject) String
Replaces the last occurrence of search with replace in subject.
reverse(String value) String
Reverses value.
slug(String title, {String separator = '-', Map<String, String>? dictionary}) String
Generates a URL-friendly slug from title.
snake(String value, [String delimiter = '_']) String
Converts value to snake_case (or with custom delimiter).
squish(String value) String
Removes all whitespace and collapses internal whitespace into single spaces.
start(String value, String prefix) String
Begins value with a single instance of prefix.
startsWith(String haystack, dynamic needles) bool
Determines if haystack starts with any of the needles.
studly(String value) String
Converts value to StudlyCase / PascalCase.
substr(String value, int start, [int? length]) String
Returns a substring of value starting at start for length chars.
substrCount(String haystack, String needle) int
Counts non-overlapping occurrences of needle in haystack.
swap(Map<String, String> map, String subject) String
Replaces multiple substrings in subject using a Map of search→replace.
take(String value, int limit) String
Returns the first limit characters of value.
title(String value) String
Converts value to "Title Case".
ucfirst(String value) String
Uppercases the first character of value.
ucsplit(String value) List<String>
Splits value into words on uppercase boundaries.
ulid() String
Generates a ULID (Universally Unique Lexicographically Sortable Identifier).
unwrap(String value, String before, [String? after]) String
Strips a single instance of before from the start and after (or before if after is null) from the end of value.
upper(String value) String
Uppercases value.
uuid() String
Generates a random UUID v4.
uuid7() String
Generates a UUID v7 (time-ordered, RFC 9562).
wordCount(String value) int
Returns the word count of value.
words(String value, [int words = 100, String end = '...']) String
Limits value to a number of words.
wrap(String value, String before, [String? after]) String
Wraps value with before and after (or just before if after is null).