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 (i < 0) {
    i = 0;
  }
  if (i > length) {
    i = length;
  }
  final start = substring(0, i);
  final end = substring(i);
  return start + value + end;
}