WMLScript Standard Libraries: The String Library - Part II
WMLScript Standard Libraries
The String Library - Part II
Here are the rest of the functions in the String
library:
removeAt()
This function returns a new string where the element and the given separator with the given index are removed from the specified string. If the index is negative, the first element is removed. If it is out of range, the last element is removed. 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.
Syntax | removeAt(string, index, separator) |
Parameters | string = String, index = Integer, separator = String |
Returns | String or invalid |
Examples: | var a = String.removeAt("Doc JavaScript is a leading source for JavaScript tips", 1, " "); // a = "Doc is a leading source for JavaScript tips" var b = String.removeAt("Doc JavaScript is a leading source for JavaScript tips", 0, "J"); // b = "avaScript is a leading source for JavaScript tips" var c = String.removeAt("Doc JavaScript is a leading source for JavaScript tips", -5, " "); // c = "JavaScript is a leading source for JavaScript tips" var d = String.removeAt("Doc JavaScript is a leading source for JavaScript tips", 12, " "); // d = "Doc JavaScript is a leading source for JavaScript " var e = String.removeAt("Doc JavaScript is a leading source for JavaScript tips", 1, ""); // e = invalid var f = String.removeAt("Doc JavaScript is a leading source for JavaScript tips", "kuku", " "); // f = invalid var g = String.removeAt("Doc JavaScript is a leading source for JavaScript tips", 0, J); // g = "avaScript is a leading source for JavaScript tips" var h = String.removeAt("Doc JavaScript is a leading source for JavaScript tips", 0, "j"); // h = "Doc JavaScript is a leading source for JavaScript tips" |
replaceAt()
This function returns a new string where the element and the given separator with the given index are replaced with a new substring. If the index is negative, the first element is replaced. If it is out of range, the last element is replaced. 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.
Syntax | replaceAt(string, subString, index, separator) |
Parameters | string = String, subString = String, index = Integer, separator = String |
Returns | String or invalid |
Examples: | var a = String.replaceAt("Doc JavaScript is a leading source for JavaScript tips", "site", 1, " "); // a = "Doc site is a leading source for JavaScript tips" var b = String.replaceAt("Doc JavaScript is a leading source for JavaScript tips", "The Doc", 0, "J"); // b = "The Doc JavaScript is a leading source for JavaScript tips" var c = String.replaceAt("Doc JavaScript is a leading source for JavaScript tips", "The Doc", -5, " "); // c = "The Doc JavaScript is a leading source for JavaScript tips" var d = String.replaceAt("Doc JavaScript is a leading source for JavaScript tips", "tips and tutorials", 12, " "); // d = "Doc JavaScript is a leading source for JavaScript tips and tutorials" var e = String.replaceAt("Doc JavaScript is a leading source for JavaScript tips", "kuku", 1, ""); // e = invalid var f = String.replaceAt("Doc JavaScript is a leading source for JavaScript tips", "foobar", "kuku", " "); // f = invalid var g = String.replaceAt("Doc JavaScript is a leading source for JavaScript tips", "The Doc", 0, J); // g = "The Doc JavaScript is a leading source for JavaScript tips" var h = String.replaceAt("Doc JavaScript is a leading source for JavaScript tips", "kuku", 0, "j"); // h = "Doc JavaScript is a leading source for JavaScript tips" |
insertAt()
This function inserts the specified substring (and the separator) before the element at the given index. If the index is negative, the substring (followed by the separator) is inserted before the first element. If it is out of range, the substring (following an additional separator) is inserted after the last element. 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.
Syntax | insertAt(string, subString, index, separator) |
Parameters | string = String, subString = String, index = Integer, separator = String |
Returns | String or invalid |
Examples: | var a = String.insertAt("Doc JavaScript is a leading source for JavaScript tips", "site", 2, " "); // a = "Doc JavaScript site is a leading source for JavaScript tips" var b = String.insertAt("Doc JavaScript is a leading source for JavaScript tips", "(JS)", 0, "J"); // b = "Doc (JS) JavaScript is a leading source for JavaScript tips" var c = String.insertAt("Doc JavaScript is a leading source for JavaScript tips", "The", -5, " "); // c = "The Doc JavaScript is a leading source for JavaScript tips" var d = String.insertAt("Doc JavaScript is a leading source for JavaScript tips", "and tutorials", 12, " "); // d = "Doc JavaScript is a leading source for JavaScript tips and tutorials" var e = String.insertAt("Doc JavaScript is a leading source for JavaScript tips", "kuku", 1, ""); // e = invalid var f = String.insertAt("Doc JavaScript is a leading source for JavaScript tips", "foobar", "kuku", " "); // f = invalid var g = String.insertAt("Doc JavaScript is a leading source for JavaScript tips", "(JS)", 0, J); // g = "Doc (JS) JavaScript is a leading source for JavaScript tips" var h = String.insertAt("Doc JavaScript is a leading source for JavaScript tips", "kuku", 0, "j"); // h = "Doc JavaScript is a leading source for JavaScript tips" |
squeeze()
This function returns a string where all white spaces are reduced to singe spaces.
Syntax | squeeze(string) |
Parameters | string = String |
Returns | String or invalid |
Examples: | var a = String.squeeze("Doc JavaScript\r\n"); // a = "Doc JavaScript " |
trim()
This function returns a string where all leading and trailing white spaces are removed.
Syntax | trim(string) |
Parameters | string = String |
Returns | String or invalid |
Examples: | var a = String.trim("Doc JavaScript\r\n"); // a = "Doc JavaScript" |
compare()
This function returns -1 if the first string is less than second string, 0 if they are identical, and 1 if the first string is greater.
Syntax | compare(string1, string2) |
Parameters | string1 = String, string2 = String |
Returns | Integer or invalid |
Examples: | var a = String.compare("Doc", "Doc"); // a = 0 var b = String.compare("Doc", "JavaScript"); // b = -1 var c = String.compare("JavaScript", "Doc"); // c = 1 |
toString()
This function returns a string representation of the given value, according to the conversion rules of WMLScript from boolean, integer, and floating-point values to strings.
Syntax | toString(value) |
Parameters | value = Any |
Returns | String or invalid |
Examples: | var a = String.toString(583.97); // a = "583.97" var b = String.toString(true); // b = "true" var c = String.toString(invalid); // c = "invalid" |
format()
This function formats a given value according to a specified format. The syntax is format(formatSpec, value). formatSpec is a string that may include additional characters beyond the format specification. If more than one format specification is given, only the left most one is used, and the remaining ones are replaced by an empty string. Illegal format specification results in a return value of invalid
. invalid is also returned when specifying a floating-point format when floating-point numbers are not supported. formatSpec has the following format:
% [width] [.precision] type
The width argument specifies the minimum number of characters printed. If the number of characters in the formatted string is smaller, blanks are added. The formatted string is never truncated. Percent characters (%) can be presented by two percent characters (%%) in the formatSpec argument.
The precision argument sets the precision of the output value. Unlike the width argument, precision argument may truncate the output value. The interpretation of this argument depends on the type argument value:
d | precision specifies the minimum number of digits to be printed. If the number of digits in value is smaller, the output value is padded on the left with zeroes. No truncation if value is larger. Default precision is 1. |
f | precision specifies the number of digits after the decimal point. At least one digit before the decimal point. The value is rounded to the appropriate number of digits. Default precision is 6. If precision is 0 or no precision specified after the period (.), no decimal point is printed. |
s | precision specifies the maximum number of characters to print. By default, all characters are printed. |
The type argument is the only required one. It determines how to interpret the formatted value. The supported options are:
d | Integer: The output value has the format of [-]dddd, where dddd is one or more decimal digits. |
f | Floating-Point: The output value has the format of [-]dddd.dddd, where dddd is one or more decimal digits. The number of digits before the decimal point depends on the magnitude of the number, while the number of digits after the decimal point depends on specified precision. If the precision argument is too larger for the formatted value, it is padded with zeroes. For example, String.format("%2.3f", 1.2) is "1.200" . |
s | String: Characters are printed up to the end of the string or until the precision value is reached. When the width argument is larger than the precision argument, the width argument is ignored. |
Here is a summary of the format()
function:
Syntax | format(formatSpec, value) |
Parameters | formatSpec = String, value = Any |
Returns | String or invalid |
Examples: | var a = String.format("result is: %4d", 32); // a = "result is: 32" var b = String.format("%4.3d", 32); // b = " 032" var c = String.format("Doc %s", "JavaScript"); // c = "Doc JavaScript" var d = String.format("%3f", 12345.3456); // d = "12345.3456" |
Next: How to use URL library - Part I
Produced by Yehuda Shiran and Tomer Shiran
Created: June 5, 2000
Revised: June 5, 2000
URL: https://www.webreference.com/js/column63/6.html