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:
- after, afterLast, before, beforeLast
- between, betweenFirst
- contains, containsAll, startsWith, endsWith
- is_, position, substrCount
- match, matchAll, excerpt
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
subjectafter the first occurrence ofsearch. Returnssubjectunchanged ifsearchis empty or not found. -
afterLast(
String subject, String search) → String -
Returns the portion of
subjectafter the last occurrence ofsearch. -
before(
String subject, String search) → String -
Returns the portion of
subjectbefore the first occurrence ofsearch. -
beforeLast(
String subject, String search) → String -
Returns the portion of
subjectbefore the last occurrence ofsearch. -
between(
String subject, String from, String to) → String -
Returns the portion of
subjectbetweenfromandto. -
betweenFirst(
String subject, String from, String to) → String -
Returns the smallest portion of
subjectbetweenfromandto. -
camel(
String value) → String -
Converts
valueto 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
haystackcontains any of theneedles. -
containsAll(
String haystack, List needles, {bool ignoreCase = false}) → bool -
Determines if
haystackcontains all theneedles. -
deduplicate(
String value, [String character = ' ']) → String -
Replaces consecutive runs of
characterwith a single instance. -
endsWith(
String haystack, dynamic needles) → bool -
Determines if
haystackends with any of theneedles. -
excerpt(
String text, String phrase, {int radius = 100, String omission = '...'}) → String? -
Extracts a snippet of
textaround the first occurrence ofphrase. -
finish(
String value, String cap) → String -
Caps
valuewith a single instance ofcap. -
headline(
String value) → String -
Converts
valueto a "Headline" — words split, then title cased. -
is_(
dynamic pattern, String value) → bool -
Determines if
valuematches the givenpattern. Asterisks are wildcards. -
isAscii(
String value) → bool -
Returns true if
valueis 7-bit ASCII. -
isJson(
String value) → bool -
Returns true if
valueis valid JSON. -
isUlid(
String value) → bool -
Returns true if
valueis a ULID (26 chars, Crockford base32). -
isUrl(
String value) → bool -
Returns true if
valueis a valid URL. -
isUuid(
String value) → bool -
Returns true if
valueis a valid UUID (any version). -
kebab(
String value) → String -
Converts
valueto 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
valuetolimitcharacters and appendsend. -
lower(
String value) → String -
Lowercases
value. -
mask(
String string, String character, int index, [int? length]) → String -
Masks a portion of
stringwithcharacter, starting atindexforlength. -
match(
Pattern pattern, String subject) → String? -
Returns the first regex match of
patterninsubject, ornull. -
matchAll(
Pattern pattern, String subject) → List< String> -
Returns all regex matches of
patterninsubject. -
padBoth(
String value, int length, [String pad = ' ']) → String -
Pads both sides of
valuewithpaduntillengthis reached. -
padLeft(
String value, int length, [String pad = ' ']) → String -
Pads the left side of
value. -
padNumber(
String value, int length) → String -
Pads
valuewith leading zeros to reachlengthcharacters. -
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
needleinhaystack, ornullif not found.offsetstarts 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
searchfromsubject. -
repeat(
String value, int times) → String -
Repeats
valuetimestimes. -
replace(
dynamic search, dynamic replace, String subject) → String -
Replaces every occurrence of
searchwithreplaceinsubject. -
replaceFirst(
String search, String replace, String subject) → String -
Replaces the first occurrence of
searchwithreplaceinsubject. -
replaceLast(
String search, String replace, String subject) → String -
Replaces the last occurrence of
searchwithreplaceinsubject. -
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
valueto snake_case (or with customdelimiter). -
squish(
String value) → String - Removes all whitespace and collapses internal whitespace into single spaces.
-
start(
String value, String prefix) → String -
Begins
valuewith a single instance ofprefix. -
startsWith(
String haystack, dynamic needles) → bool -
Determines if
haystackstarts with any of theneedles. -
studly(
String value) → String -
Converts
valueto StudlyCase / PascalCase. -
substr(
String value, int start, [int? length]) → String -
Returns a substring of
valuestarting atstartforlengthchars. -
substrCount(
String haystack, String needle) → int -
Counts non-overlapping occurrences of
needleinhaystack. -
swap(
Map< String, String> map, String subject) → String -
Replaces multiple substrings in
subjectusing aMapof search→replace. -
take(
String value, int limit) → String -
Returns the first
limitcharacters ofvalue. -
title(
String value) → String -
Converts
valueto "Title Case". -
ucfirst(
String value) → String -
Uppercases the first character of
value. -
ucsplit(
String value) → List< String> -
Splits
valueinto 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
beforefrom the start andafter(orbeforeifafteris null) from the end ofvalue. -
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
valueto a number ofwords. -
wrap(
String value, String before, [String? after]) → String -
Wraps
valuewithbeforeandafter(or justbeforeifafteris null).