Skip to content

StringOps

extensionStringOpsonString

Scala-style string operations, treating a String as a sequence of single-character strings.

Properties

head extension no setter

Stringgethead

The first character. Throws RangeError if the string is empty.

Available on String, provided by the StringOps extension

Implementation
dart
String get head => nonEmpty ? this[0] : throw RangeError('.head on empty string');

headOption extension no setter

Option<String>getheadOption

The first character as Some, or None if the string is empty.

Available on String, provided by the StringOps extension

Implementation
dart
Option<String> get headOption => nonEmpty ? Some(this[0]) : none();

init extension no setter

Stringgetinit

All characters except the last. Returns an empty string if already empty.

Available on String, provided by the StringOps extension

Implementation
dart
String get init => !isEmpty ? substring(0, length - 1) : this;

inits extension no setter

RIterator<String>getinits

An iterator over successive init prefixes, from longest to shortest, ending with an empty string.

Available on String, provided by the StringOps extension

Implementation
dart
RIterator<String> get inits => _iterateUntilEmpty((s) => s.init);

last extension no setter

Stringgetlast

The last character. Throws RangeError if the string is empty.

Available on String, provided by the StringOps extension

Implementation
dart
String get last => nonEmpty ? this[length - 1] : throw RangeError('.last on empty string');

lastOption extension no setter

Option<String>getlastOption

The last character as Some, or None if the string is empty.

Available on String, provided by the StringOps extension

Implementation
dart
Option<String> get lastOption => nonEmpty ? Some(this[length - 1]) : none();

nonEmpty extension no setter

boolgetnonEmpty

Returns true if the string is not empty.

Available on String, provided by the StringOps extension

Implementation
dart
bool get nonEmpty => !isEmpty;

riterator extension no setter

RIterator<String>getriterator

An RIterator over the individual characters of this string.

Available on String, provided by the StringOps extension

Implementation
dart
RIterator<String> get riterator => RIterator.tabulate(length, (ix) => this[ix]);

tail extension no setter

Stringgettail

All characters except the first. Throws RangeError if the string is empty.

Available on String, provided by the StringOps extension

Implementation
dart
String get tail => nonEmpty ? substring(1, length) : throw RangeError('.tail on empty string');

tails extension no setter

RIterator<String>gettails

An iterator over successive tail suffixes, from longest to shortest, ending with an empty string.

Available on String, provided by the StringOps extension

Implementation
dart
RIterator<String> get tails => _iterateUntilEmpty((s) => s.tail);

Methods

drop() extension

Stringdrop(intn)

Returns all characters after the first n, or an empty string if n >= length.

Available on String, provided by the StringOps extension

Implementation
dart
String drop(int n) => slice(min(n, length), length);

dropRight() extension

StringdropRight(intn)

Returns all characters except the last n, or an empty string if n >= length.

Available on String, provided by the StringOps extension

Implementation
dart
String dropRight(int n) => take(length - max(n, 0));

dropWhile() extension

StringdropWhile(boolFunction(String)p)

Drops the longest prefix of characters satisfying p and returns the remainder.

Available on String, provided by the StringOps extension

Implementation
dart
String dropWhile(Function1<String, bool> p) => switch (indexWhere((c) => !p(c))) {
  -1 => '',
  final x => substring(x),
};

exists() extension

boolexists(boolFunction(String)p)

Returns true if at least one character satisfies p.

Available on String, provided by the StringOps extension

Implementation
dart
bool exists(Function1<String, bool> p) => indexWhere(p) != -1;

filter() extension

Stringfilter(boolFunction(String)p)

Returns a new string containing only the characters satisfying p.

Available on String, provided by the StringOps extension

Implementation
dart
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

StringfilterNot(boolFunction(String)p)

Returns a new string containing only the characters not satisfying p.

Available on String, provided by the StringOps extension

Implementation
dart
String filterNot(Function1<String, bool> p) => filter((c) => !p(c));

find() extension

Option<String>find(boolFunction(String)p)

Returns the first character satisfying p as Some, or None if no character satisfies p.

Available on String, provided by the StringOps extension

Implementation
dart
Option<String> find(Function1<String, bool> p) => switch (indexWhere(p)) {
  -1 => none(),
  final x => Some(this[x]),
};

fold() extension

Stringfold(Stringz,StringFunction(String,String)op)

Folds characters left-to-right using op, starting from z.

Available on String, provided by the StringOps extension

Implementation
dart
String fold(String z, Function2<String, String, String> op) => foldLeft(z, op);

foldLeft() extension

BfoldLeft<B>(Bz,BFunction(B,String)op)

Left-associative fold over characters, starting from z.

Available on String, provided by the StringOps extension

Implementation
dart
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

BfoldRight<B>(Bz,BFunction(String,B)op)

Right-associative fold over characters, starting from z.

Available on String, provided by the StringOps extension

Implementation
dart
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

boolforall(boolFunction(String)f)

Returns true if all characters satisfy f.

Available on String, provided by the StringOps extension

Implementation
dart
bool forall(Function1<String, bool> f) {
  var i = 0;

  while (i < length) {
    if (!f(this[i])) return false;
    i += 1;
  }

  return true;
}

foreach() extension

voidforeach(voidFunction(String)f)

Applies f to each character.

Available on String, provided by the StringOps extension

Implementation
dart
void foreach(Function1<String, void> f) {
  var i = 0;

  while (i < length) {
    f(this[i]);
    i += 1;
  }
}

grouped() extension

RIterator<String>grouped(intsize)

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
dart
RIterator<String> grouped(int size) => _StringGroupedIterator(this, size);

indexWhere() extension

intindexWhere(boolFunction(String)p, [intend=2147483647])

Returns the index of the first character satisfying p, or -1 if none does.

Available on String, provided by the StringOps extension

Implementation
dart
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

intlastIndexWhere(boolFunction(String)p, [intend=2147483647])

Returns the index of the last character satisfying p, or -1 if none does.

Available on String, provided by the StringOps extension

Implementation
dart
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

Recordpartition(boolFunction(String)p)

Splits characters into two strings: those satisfying p (first) and those not satisfying p (second).

Available on String, provided by the StringOps extension

Implementation
dart
(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

boolregionMatches(inttoffset,Stringother,intooffset,intlen)

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
dart
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

boolregionMatchesIgnoreCase(inttoffset,Stringother,intooffset,intlen,);

Case-insensitive variant of regionMatches. Returns false if any index is out of bounds.

Available on String, provided by the StringOps extension

Implementation
dart
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

Stringslice(intfrom,intuntil)

Returns the substring from index from (inclusive) to until (exclusive), clamped to valid bounds.

Available on String, provided by the StringOps extension

Implementation
dart
String slice(int from, int until) {
  final start = max(from, 0);
  final end = min(until, length);

  return start >= end ? '' : substring(start, end);
}

sliding() extension

RIterator<String>sliding(intsize, [intstep=1])

Returns an iterator of overlapping substrings of length size, advancing by step characters each time.

Available on String, provided by the StringOps extension

Implementation
dart
RIterator<String> sliding(int size, [int step = 1]) =>
    riterator.sliding(size, step).map((s) => s.mkString());

span() extension

Recordspan(boolFunction(String)p)

Returns the longest prefix of characters satisfying p paired with the remainder.

Available on String, provided by the StringOps extension

Implementation
dart
(String, String) span(Function1<String, bool> p) => switch (indexWhere((c) => !p(c))) {
  -1 => (this, ''),
  final x => (substring(0, x), substring(x)),
};

splitAt() extension

RecordsplitAt(intn)

Splits the string at index n, returning (take(n), drop(n)).

Available on String, provided by the StringOps extension

Implementation
dart
(String, String) splitAt(int n) => (take(n), drop(n));

stripPrefix() extension

StringstripPrefix(Stringprefix)

Removes prefix from the start of this string if present; otherwise returns this string unchanged.

Available on String, provided by the StringOps extension

Implementation
dart
String stripPrefix(String prefix) => startsWith(prefix) ? substring(prefix.length) : this;

stripSuffix() extension

StringstripSuffix(Stringsuffix)

Removes suffix from the end of this string if present; otherwise returns this string unchanged.

Available on String, provided by the StringOps extension

Implementation
dart
String stripSuffix(String suffix) =>
    endsWith(suffix) ? substring(0, length - suffix.length) : this;

take() extension

Stringtake(intn)

Returns the first n characters, or the whole string if n >= length.

Available on String, provided by the StringOps extension

Implementation
dart
String take(int n) => slice(0, min(n, length));

takeRight() extension

StringtakeRight(intn)

Returns the last n characters, or the whole string if n >= length.

Available on String, provided by the StringOps extension

Implementation
dart
String takeRight(int n) => drop(length - max(n, 0));

takeWhile() extension

StringtakeWhile(boolFunction(String)p)

Returns the longest prefix of characters satisfying p.

Available on String, provided by the StringOps extension

Implementation
dart
String takeWhile(Function1<String, bool> p) => switch (indexWhere((c) => !p(c))) {
  -1 => this,
  final x => substring(0, x),
};