Strings class
A set of String utility functions that aim to extend the set of functions available the core String class as well as provding safe methods when working with nullable Strings. A Strings method will never thrown an NPE and aims to provide the expected result by treating the null as an empty String or a space filled String where a range access is applied.
The same approach is applied for non-null Strings when the strings length would result in a RangeError; Instead we pad the String to ensure code returns the expected length String and you code keesp running rather than crashing.
e.g.
Strings.substring(null, 2,3);
-> ' '
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
-
abbreviate(
String? string, int maxWidth, {int offset = 0}) → String -
Methods that deal with parts of a string.
Abbreviate a string to
maxWidthby truncating the string and adding '...' to the truncated string. -
allMatches(
String? pattern, String string, [int start = 0]) → Iterable< Match> - Refer to String.allMatches
-
codeUnitAt(
String? string, int index) → int -
Refer to
Safe.codeUnitAt -
codeUnits(
String? string) → List< int> - Refer to String.codeUnits
-
compareTo(
String? string, String? other, {bool nullIsLessThan = true}) → int -
Refer to
Safe.compareTo -
conjuctionJoin(
List< String> list, {String separator = ', ', String conjuction = ' or '}) → String -
Concates a list by inserting
separatorbetween each item in the list, except for the last separator whereconjuctionis used instead. If you don't passseparatorthen ',' is used. If you don't passconjuctionthen 'or' is used. If thelistis empty then an empty String is returned. -
contains(
String? string, Pattern other, [int startIndex = 0]) → bool -
Refer to
Safe.contains -
endsWith(
String? string, String? other) → bool -
Refer to
Safe.endsWith -
equals(
String? lhs, String? rhs) → bool - Safely compares two nullable strings.
-
equalsIgnoreCase(
String? lhs, String? rhs) → bool - Compare two nullable strings ignoring case.
-
hidePart(
String? string, {int start = 0, int? end, String replaceWith = '*'}) → String -
Obscures part of
stringby replace the characters betweenstart(inclusive) andendexclusive withreplaceWith -
indexOf(
String? string, Pattern pattern, [int start = 0]) → int -
Refer to
Safe.indexOf -
isAscii(
String? string) → bool -
returns true if
stringonly contains ascii characters. (0 - 128) -
isBlank(
String? string) → bool -
Returns true if the
stringis null or Blank. -
isDigits(
String? string) → bool -
returns true if
stringonly contains digits -
isEmpty(
String? string) → bool -
true if the
stringis null, or is a zero length String -
isLowerCase(
String? string) → bool -
Returns true if
stringdoes not contain upper case letters -
isNotBlank(
String? string) → bool -
Returns true if the
stringis not null and not Blank. -
isNotEmpty(
String? string) → bool -
true if the
stringis not null and is not a zero length String -
isNumeric(
String? string) → bool -
Checks if
stringis a number by attempting to parse it as a double. -
isPrintable(
int? character) → bool -
Returns true if
characteris a printable ascii character. -
isUpperCase(
String? string) → bool -
Returns true if
stringdoes not contain any lower case letters. -
isWhitespace(
String string) → bool -
returns true if the
stringonly contains whitespace characters. We use the same definition of whitespace as String.trim. -
isWhitespaceRune(
int rune) → bool -
returns true if the
runeis a whitespace characters. We use the same definition of whitespace as String.trim. -
join(
List< Object?> ? list, {String separator = '', bool excludeEmpty = false}) → String -
Returns the joined elements of the
list. -
lastIndexOf(
String? string, Pattern pattern, [int? start]) → int -
Refer to
Safe.lastIndexOf -
left(
String? string, int take, {Pad pad = Pad.none}) → String -
Returns left most
takecharacters fromstring. -
length(
String? string) → int - Refer to String.length
-
matchAsPrefix(
String? pattern, String string, [int start = 0]) → Match? -
Refer to
Safe.matchAsPrefix -
orElse(
String? string, String elsestring) → String -
If
stringis not null, then returnstringIfstringis null theelsestringis returned -
orElseCall(
String? string, String elsestring()) → String -
If
stringis not null, then returnstringIfstringis null, callselsestringand return the result. -
orElseOnBlank(
String? string, String elseString) → String -
If the
stringis not blank then we returnstringotherwise we returnelseString -
padLeft(
String? string, int width, [String padding = ' ']) → String -
Refer to
Safe.padLeft -
padRight(
String? string, int width, [String padding = ' ']) → String -
Refer to
Safe.padRight -
replaceAll(
String? string, Pattern from, String replace) → String -
Refer to
Safe.replaceAll -
replaceAllMapped(
String? string, Pattern from, String replace(Match match)) → String -
Refer to
Safe.replaceAllMapped -
replaceFirst(
String? string, Pattern from, String to, [int startIndex = 0]) → String -
Refer to
Safe.replaceFirst -
replaceFirstMapped(
String? string, Pattern from, String replace(Match match), [int startIndex = 0]) → String -
Refer to
Safe.replaceFirstMapped -
replaceRange(
String? string, int start, int? end, String replacement) → String -
Refer to
Safe.replaceRange -
reverse(
String? string) → String - Returns a string with reversed order of characters.
-
right(
String? string, int take, {Pad pad = Pad.none}) → String -
Returns the right most
takecharacters fromstring. -
runes(
String? string) → Runes - Refer to String.runes
-
split(
String? string, Pattern pattern) → List< String> -
Refer to
Safe.split -
splitMapJoin(
String? string, Pattern pattern, {String onMatch(Match)?, String onNonMatch(String)?}) → String -
Refer to
Safe.splitMapJoin -
startsWith(
String? string, Pattern pattern, [int index = 0]) → bool -
Refer to
Safe.startsWith -
startsWithLowerCase(
String? string) → bool -
Returns true if
stringstarts with the lower case character. -
startsWithUpperCase(
String? string) → bool - Returns true if the string starts with the upper case character.
-
substring(
String? string, int start, [int? end]) → String -
Refer to
Safe.substring -
toCamelCase(
String? string, {bool lower = false}) → String -
Returns
stringin the form "UpperCamelCase" or "lowerCamelCase". -
toCapitalised(
String? string) → String -
Returns
stringwith the first character capitalized. -
toEmpty(
String? string) → String -
If
stringis null we return a zero length string otherwise we returnstring. -
toEscaped(
String? string, {String encode(int charCode)?}) → String - Returns an escaped string.
-
toLowerCase(
String? string) → String -
Refer to
Safe.toLowerCase -
toPrintable(
String? string) → String - Returns an escaped string.
-
toProperCase(
String? sentence) → String -
Converts
sentenceto proper case by capitalising the first letter of each word and forcing all other characters to lower case. -
toSnakeCase(
String? string) → String -
Converts
stringto snake_case by inserting an underscore before each sequence of upper case letters and changing all upper case letters to lowercase. -
toUnicode(
int? charCode) → String - Returns an Unicode representation of the character code.
-
toUpperCase(
String? string) → String -
Refer to
Safe.toUpperCase -
trim(
String? string) → String -
Refer to
Safe.trim -
trimLeft(
String? string) → String -
Refer to
Safe.trimLeft -
trimRight(
String? string) → String -
Refer to
Safe.trimRight -
upTo(
String string, String delimiter) → String -
Returns the left most part of a string upto,
but not including, the
delimiter -
within(
String string, String left, String right) → String -
Returns the string bounded by the
leftandrightdelimiters. Throws an ArgumentError exception if either of the delimiters are missing. If there are nested delimiters we return the outer most delimiters.