Instant methods: insert()

The instant method insert() inserts a string operand into another string operand and returns the resulting string.

Declaration

string string.insert( string aString, int32 aIndex )

Parameters

aString

The string to insert.

aIndex

The character position within the original string where to insert aString. The first character has the index 0, the second character 1, and so far.

Discussion

The insert() method inserts the string passed in the parameter aString into the string, in context of which the method has been called. The string is inserted at the position specified in the parameter aIndex. The method returns the new string without modifying any of the both original string operands.

If aIndex is <= 0 (zero), aString is prepended just at the beginning of the original string. If aIndex is greater than or equal to the length of the original string, aString is simply appended at the end of this string. For example:

var string s1 = "green blue"; var string s2 = "red "; var string result = s1.insert( s2, 6 ); // result = "green red blue" var string result = s1.insert( s2, -10 ); // result = "red green blue" var string result = s1.insert( s2, 100 ); // result = "green bluered "