insertAt method
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;
}