O'Reilly logo

Learning WML, and WMLScript by Martin Frost

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required