WMLScript Standard Libraries: The String Library - Part I | WebReference

WMLScript Standard Libraries: The String Library - Part I


WMLScript Standard Libraries

The String Library - Part I

The 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). Here are the functions included in the String library:

length()

This function returns the length of the given string.

Syntaxlength(string)
Parametersstring = String
ReturnsInteger or invalid
Examples:var a = String.length("foobar"); // a = 6
var b = String.length(""); // b = 0
var c = String.length(85.397); // c = 6

isEmpty()

This function returns true if the given string is empty, false otherwise.

SyntaxisEmpty(string)
Parametersstring = String
ReturnsBoolean or invalid
Examples:var a = String.isEmpty("foobar"); // a = false
var b = String.isEmpty(""); // b = true
var c = String.isEmpty(true); // c = false

charAt()

This function returns a new string of length one, containing the character at the specified index of the given string. An empty string is returned if the given index is out of range.

SyntaxcharAt(string, index)
Parametersstring = String, index = Integer
ReturnsString or invalid
Examples:var a = String.charAt("foobar", 3); // a = "b"
var b = String.charAt("foobar", 9); // b = ""
var c = String.charAt("foobar", "last"); // c = invalid

subString()

This function returns a new string of the specified length, starting from the specified index of the given string. If the specified index is negative, an index 0 is used. If it is out of range, an empty string is returned. If the specified length is less than or equal to 0, an empty string is returned. If it is out of range, the remaining characters to the end of the string are returned. If the specified index is a floating-point number, Float.int() is used to convert it to an integer.

SyntaxsubString(string, startIndex, length)
Parametersstring = String, startIndex = Integer, length = Integer
ReturnsString or invalid
Examples:var a = String.subString("foobar", 3, 2); // a = "ba"
var b = String.subString("foobar", 0, 3); // b = "foo"
var c = String.subString("foobar", -3, 4); // c = "foob"
var d = String.subString("foobar", 2, 8); // d = "obar"
var e = String.subString("foobar", 9, 4); // e = ""
var f = String.subString("foobar", 2, -6); // f = ""
var g = String.subString("foobar", "three", 4); // g = invalid

find()

This function returns the index of the first character in the given string that matches the specified substring. No case folding is performed. A value of -1 is returned if no match is found. invalid is returned if given substring is empty.

Syntaxfind(string, subString)
Parametersstring = String, subString = String
ReturnsInteger or invalid
Examples:var a = String.find("foobar", "bar"); // a = 3
var b = String.find("foobar", "kuku"); // b = -1
var c = String.find("foobar", ""); // c = invalid
var d = String.subString("foobar", foo); // d = 0

replace()

This function returns a new string which is the result of replacing a given old string with another specified new string. No case folding is done. If the specified old string is empty, invalid is returned.

Syntaxreplace(string, oldString, newString)
Parametersstring = String, oldString = String, newString = String
ReturnsString or invalid
Examples:var a = String.replace("foobar", "foo", "kuku"); // a = "kukubar"
var b = String.replace("foobar", "", "kuku"); // b = invalid

elements()

This function returns the number of elements in the given string separated by the given separator. Returns invalid if the given separator is empty.

Syntaxelements(string, separator)
Parametersstring = String, separator = String
ReturnsInteger or invalid
Examples:var a = String.elements("Doc JavaScript is a leading source for JavaScript tips", " "); // a = 9
var b = String.elements("Doc JavaScript is a leading source for JavaScript tips", "J"); // b = 2
var c = String.elements("foobar", ""); // c = invalid

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 = ""

Next: How to use String library - Part II

https://www.internet.com

Produced by Yehuda Shiran and Tomer Shiran

Created: June 5, 2000
Revised: June 5, 2000

URL: https://www.webreference.com/js/column63/5.html