StringOps
Scala-style string operations, treating a String as a sequence of single-character strings.
Properties
head extension no setter
The first character. Throws RangeError if the string is empty.
Available on String, provided by the StringOps extension
Implementation
String get head => nonEmpty ? this[0] : throw RangeError('.head on empty string');headOption extension no setter
The first character as Some, or None if the string is empty.
Available on String, provided by the StringOps extension
Implementation
Option<String> get headOption => nonEmpty ? Some(this[0]) : none();init extension no setter
All characters except the last. Returns an empty string if already empty.
Available on String, provided by the StringOps extension
Implementation
String get init => !isEmpty ? substring(0, length - 1) : this;inits extension no setter
An iterator over successive init prefixes, from longest to shortest, ending with an empty string.
Available on String, provided by the StringOps extension
Implementation
RIterator<String> get inits => _iterateUntilEmpty((s) => s.init);last extension no setter
The last character. Throws RangeError if the string is empty.
Available on String, provided by the StringOps extension
Implementation
String get last => nonEmpty ? this[length - 1] : throw RangeError('.last on empty string');lastOption extension no setter
The last character as Some, or None if the string is empty.
Available on String, provided by the StringOps extension
Implementation
Option<String> get lastOption => nonEmpty ? Some(this[length - 1]) : none();nonEmpty extension no setter
Returns true if the string is not empty.
Available on String, provided by the StringOps extension
Implementation
bool get nonEmpty => !isEmpty;riterator extension no setter
An RIterator over the individual characters of this string.
Available on String, provided by the StringOps extension
Implementation
RIterator<String> get riterator => RIterator.tabulate(length, (ix) => this[ix]);tail extension no setter
All characters except the first. Throws RangeError if the string is empty.
Available on String, provided by the StringOps extension
Implementation
String get tail => nonEmpty ? substring(1, length) : throw RangeError('.tail on empty string');tails extension no setter
An iterator over successive tail suffixes, from longest to shortest, ending with an empty string.
Available on String, provided by the StringOps extension
Implementation
RIterator<String> get tails => _iterateUntilEmpty((s) => s.tail);Methods
drop() extension
Returns all characters after the first n, or an empty string if n >= length.
Available on String, provided by the StringOps extension
Implementation
String drop(int n) => slice(min(n, length), length);dropRight() extension
Returns all characters except the last n, or an empty string if n >= length.
Available on String, provided by the StringOps extension
Implementation
String dropRight(int n) => take(length - max(n, 0));dropWhile() extension
Drops the longest prefix of characters satisfying p and returns the remainder.
Available on String, provided by the StringOps extension
Implementation
String dropWhile(Function1<String, bool> p) => switch (indexWhere((c) => !p(c))) {
-1 => '',
final x => substring(x),
};exists() extension
Returns true if at least one character satisfies p.
Available on String, provided by the StringOps extension
Implementation
bool exists(Function1<String, bool> p) => indexWhere(p) != -1;filter() extension
Returns a new string containing only the characters satisfying p.
Available on String, provided by the StringOps extension
Implementation
String filter(Function1<String, bool> p) {
final buf = StringBuffer();
var i = 0;
while (i < length) {
if (p(this[i])) buf.write(this[i]);
i += 1;
}
return length == buf.length ? this : buf.toString();
}filterNot() extension
Returns a new string containing only the characters not satisfying p.
Available on String, provided by the StringOps extension
Implementation
String filterNot(Function1<String, bool> p) => filter((c) => !p(c));find() extension
Returns the first character satisfying p as Some, or None if no character satisfies p.
Available on String, provided by the StringOps extension
Implementation
Option<String> find(Function1<String, bool> p) => switch (indexWhere(p)) {
-1 => none(),
final x => Some(this[x]),
};fold() extension
Folds characters left-to-right using op, starting from z.
Available on String, provided by the StringOps extension
Implementation
String fold(String z, Function2<String, String, String> op) => foldLeft(z, op);foldLeft() extension
Left-associative fold over characters, starting from z.
Available on String, provided by the StringOps extension
Implementation
B foldLeft<B>(B z, Function2<B, String, B> op) {
var v = z;
var i = 0;
while (i < length) {
v = op(v, this[i]);
i += 1;
}
return v;
}foldRight() extension
Right-associative fold over characters, starting from z.
Available on String, provided by the StringOps extension
Implementation
B foldRight<B>(B z, Function2<String, B, B> op) {
var v = z;
var i = length - 1;
while (i >= 0) {
v = op(this[i], v);
i -= 1;
}
return v;
}forall() extension
Returns true if all characters satisfy f.
Available on String, provided by the StringOps extension
Implementation
bool forall(Function1<String, bool> f) {
var i = 0;
while (i < length) {
if (!f(this[i])) return false;
i += 1;
}
return true;
}foreach() extension
Applies f to each character.
Available on String, provided by the StringOps extension
Implementation
void foreach(Function1<String, void> f) {
var i = 0;
while (i < length) {
f(this[i]);
i += 1;
}
}grouped() extension
Returns an iterator that yields non-overlapping substrings of length size. The last group may be shorter than size if the string length is not a multiple of size.
Available on String, provided by the StringOps extension
Implementation
RIterator<String> grouped(int size) => _StringGroupedIterator(this, size);indexWhere() extension
Returns the index of the first character satisfying p, or -1 if none does.
Available on String, provided by the StringOps extension
Implementation
int indexWhere(Function1<String, bool> p, [int end = 2147483647]) {
var i = 0;
while (i < length) {
if (p(this[i])) return i;
i += 1;
}
return -1;
}lastIndexWhere() extension
Returns the index of the last character satisfying p, or -1 if none does.
Available on String, provided by the StringOps extension
Implementation
int lastIndexWhere(Function1<String, bool> p, [int end = 2147483647]) {
var i = min(end, length - 1);
while (i >= 0) {
if (p(this[i])) return i;
i -= 1;
}
return -1;
}partition() extension
Splits characters into two strings: those satisfying p (first) and those not satisfying p (second).
Available on String, provided by the StringOps extension
Implementation
(String, String) partition(Function1<String, bool> p) {
final (res1, res2) = (StringBuffer(), StringBuffer());
var i = 0;
while (i < length) {
p(this[i]) ? res1.write(this[i]) : res2.write(this[i]);
i += 1;
}
return (res1.toString(), res2.toString());
}regionMatches() extension
Returns true if the specified subregion of this string matches the specified subregion of other. Returns false if any index is out of bounds.
Available on String, provided by the StringOps extension
Implementation
bool regionMatches(int toffset, String other, int ooffset, int len) {
if (toffset < 0 || ooffset < 0 || toffset + len > length || ooffset + len > other.length) {
return false;
} else {
return substring(toffset, toffset + len) == other.substring(ooffset, ooffset + len);
}
}regionMatchesIgnoreCase() extension
Case-insensitive variant of regionMatches. Returns false if any index is out of bounds.
Available on String, provided by the StringOps extension
Implementation
bool regionMatchesIgnoreCase(int toffset, String other, int ooffset, int len) {
if (toffset < 0 || ooffset < 0 || toffset + len > length || ooffset + len > other.length) {
return false;
} else {
return substring(toffset, toffset + len).toLowerCase() ==
other.substring(ooffset, ooffset + len).toLowerCase();
}
}slice() extension
Returns the substring from index from (inclusive) to until (exclusive), clamped to valid bounds.
Available on String, provided by the StringOps extension
Implementation
String slice(int from, int until) {
final start = max(from, 0);
final end = min(until, length);
return start >= end ? '' : substring(start, end);
}sliding() extension
Returns an iterator of overlapping substrings of length size, advancing by step characters each time.
Available on String, provided by the StringOps extension
Implementation
RIterator<String> sliding(int size, [int step = 1]) =>
riterator.sliding(size, step).map((s) => s.mkString());span() extension
Returns the longest prefix of characters satisfying p paired with the remainder.
Available on String, provided by the StringOps extension
Implementation
(String, String) span(Function1<String, bool> p) => switch (indexWhere((c) => !p(c))) {
-1 => (this, ''),
final x => (substring(0, x), substring(x)),
};splitAt() extension
Splits the string at index n, returning (take(n), drop(n)).
Available on String, provided by the StringOps extension
Implementation
(String, String) splitAt(int n) => (take(n), drop(n));stripPrefix() extension
Removes prefix from the start of this string if present; otherwise returns this string unchanged.
Available on String, provided by the StringOps extension
Implementation
String stripPrefix(String prefix) => startsWith(prefix) ? substring(prefix.length) : this;stripSuffix() extension
Removes suffix from the end of this string if present; otherwise returns this string unchanged.
Available on String, provided by the StringOps extension
Implementation
String stripSuffix(String suffix) =>
endsWith(suffix) ? substring(0, length - suffix.length) : this;take() extension
Returns the first n characters, or the whole string if n >= length.
Available on String, provided by the StringOps extension
Implementation
String take(int n) => slice(0, min(n, length));takeRight() extension
Returns the last n characters, or the whole string if n >= length.
Available on String, provided by the StringOps extension
Implementation
String takeRight(int n) => drop(length - max(n, 0));takeWhile() extension
Returns the longest prefix of characters satisfying p.
Available on String, provided by the StringOps extension
Implementation
String takeWhile(Function1<String, bool> p) => switch (indexWhere((c) => !p(c))) {
-1 => this,
final x => substring(0, x),
};