Name

insertAt — Insert a new item into a list that is stored as a string

Usage

String.insertAt(str, new, index, sep)

str : String

new : String

index : Number

sep : String

Description

(See elementAt( )’s description for an explanation of how a string is processed as a list of “elements.”)

Starting with the list stored as string str, returns a list in which new has been inserted at position index. (The first element has index zero.) The return value is a new string; str itself remains unmodified.

If index is less than zero, new is added at the beginning of the list. Similarly, if index is past the end of the list, new is added at the end. Otherwise, the index of the newly inserted element is index. If index is a floating-point value, it’s first converted to an integer with Float.int( ).

Returns invalid if any of the arguments can’t be converted to the appropriate types or if sep is the empty string.

Examples

insertAt("a;b;c;d", "x", 2, ";")

returns string "a;b;x;c;d"

insertAt("a;b;c;d", "x", -1.6, ";")

returns string "x;a;b;c;d"

insertAt("a;b;c;d", "x", 17, ";")

returns string "a;b;c;d;x"

insertAt(invalid, "x", 1, "x")

returns invalid

insertAt("foo", invalid, 2, "x")

returns invalid

insertAt("foo", "x", "three", "x")

returns invalid

insertAt("foo", "x", 4, invalid)

returns invalid

insertAt("foo", "x", 5, "")

returns invalid

Get Learning WML, and WMLScript now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.