insertAt method

String insertAt(
  1. int i,
  2. String value
)

Inserts a String at the specified index.

If the String is null, an ArgumentError is thrown.

Example

String text = 'hello world';
String newText = text.insertAt(5, '!');
print(newText); // prints 'hello! world'

Implementation

String insertAt(int i, String value) {
  if (this == null) {
    throw ArgumentError('String is null');
  }
  if (i < 0 || i > this!.length) {
    throw RangeError('Index out of range');
  }
  final start = this!.substring(0, i);
  final end = this!.substring(i);
  return start + value + end;
}