insert method

String insert(
  1. String newChar,
  2. int position
)

Inserts newChar at the specified position.

Returns the original string if position is out of bounds.

Implementation

String insert(String newChar, int position) {
  // Validate the insertion position.
  if (position < 0 || position > length) return this;
  // Construct the new string.
  return substring(0, position) + newChar + substring(position);
}