Skip to content

StringOps

extension StringOps on String

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

Properties

head extension no setter

String get head

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> get headOption

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

String get init

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> get inits

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

String get last

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> get lastOption

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

bool get nonEmpty

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> get riterator

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

String get tail

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> get tails

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

String drop(int n)

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

String dropRight(int n)

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

String dropWhile(bool Function(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

bool exists(bool Function(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

String filter(bool Function(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

String filterNot(bool Function(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(bool Function(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

String fold(String z, String Function(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

B foldLeft<B>(B z, B Function(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

B foldRight<B>(B z, B Function(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

bool forall(bool Function(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

void foreach(void Function(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(int size)

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

int indexWhere(bool Function(String) p, [int end = 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

int lastIndexWhere(bool Function(String) p, [int end = 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

Record partition(bool Function(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

bool regionMatches(int toffset, String other, int ooffset, int len)

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

bool regionMatchesIgnoreCase(int toffset, String other, int ooffset, int len)

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

String slice(int from, int until)

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(int size, [int step = 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

Record span(bool Function(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

Record splitAt(int n)

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

String stripPrefix(String prefix)

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

String stripSuffix(String suffix)

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

String take(int n)

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

String takeRight(int n)

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

String takeWhile(bool Function(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),
};