insert method

String insert(
  1. int index,
  2. String str
)

Inserts the string str at the specified index within the original string. Throws RangeError if index is out of bounds.

Example:

print('Hello World'.insert(6, 'Beautiful ')); // Output: 'Hello Beautiful World'

Implementation

String insert(int index, String str) {
  if (index < 0 || index > length) {
    throw RangeError('Index out of bounds');
  }
  return '${substring(0, index)}$str${substring(index)}';
}