July 1, 2000 - WMLScript's String Library | WebReference

July 1, 2000 - WMLScript's String Library

Yehuda Shiran July 1, 2000
WMLScript's String Library
Tips: July 2000

Yehuda Shiran, Ph.D.
Doc JavaScript

The WMLScript String library includes a set of string functions that are part of the JavaScript language. A string in WMLScript is an array of characters. The first character has an index of 0. A string may be divided into elements by a special user-defined separator. A white-space character is either TAB (Horizontal Tabulation), VT (Vertical Tabulation), FF (Form Feed), SP (Space), LF (Line Feed), or CR (Carriage Return). The following functions are part of the String library: length(), isEmpty(), charAt(), subString(), find(), replace(), elements(), elementAt(), removeAt(), replaceAt(), insertAt(), squeeze(), trim(), compare(), toString(), and format().

The concept of elements is unique to WMLScript. You can split a string to its elements (separated by a user-given character), replace elements with other elements, remove them, insert new ones, etc. Go to Column 63, The String Library, to see a detailed definition of each function, including examples. Here is the definition of the elementAt() function:

elementAt()

This function first separates the given string into elements according to the specified separator, and then returns the element with the given index. If the index is negative, the first element is returned. If it is out of range, the last element is returned. If the string is empty, an empty string is returned. If the separator is empty, invalid is returned. If the index is floating-point, Float.int() is used to convert it to an integer. No case folding is done.

SyntaxelementAt(string, index, separator)
Parametersstring = String, index = Integer, separator = String
ReturnsString or invalid
Examples:var a = String.elementAt("Doc JavaScript is a leading source for JavaScript tips", 1, " "); // a = "JavaScript"
var b = String.elementAt("Doc JavaScript is a leading source for JavaScript tips", 0, "J"); // b = "Doc "
var c = String.elementAt("Doc JavaScript is a leading source for JavaScript tips", -5, " "); // c = "Doc"
var d = String.elementAt("Doc JavaScript is a leading source for JavaScript tips", 12, " "); // d = "tips"
var e = String.elementAt("Doc JavaScript is a leading source for JavaScript tips", 1, ""); // e = invalid
var f = String.elementAt("Doc JavaScript is a leading source for JavaScript tips", "kuku", " "); // f = invalid
var g = String.elementAt("Doc JavaScript is a leading source for JavaScript tips", 0, J); // g = "Doc "
var h = String.elementAt("Doc JavaScript is a leading source for JavaScript tips", 0, "j"); // h = ""