home / experts / javascript / column63 |
|
The String Library - Part IThe
|
| Syntax | length(string) |
| Parameters | string = String |
| Returns | Integer or invalid |
| Examples: | var a = String.length("foobar"); // a = 6var b = String.length(""); // b = 0var c = String.length(85.397); // c = 6 |
isEmpty()This function returns true if the given string is empty, false otherwise.
| Syntax | isEmpty(string) |
| Parameters | string = String |
| Returns | Boolean or invalid |
| Examples: | var a = String.isEmpty("foobar"); // a = falsevar b = String.isEmpty(""); // b = truevar 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.
| Syntax | charAt(string, index) |
| Parameters | string = String, index = Integer |
| Returns | String 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.
| Syntax | subString(string, startIndex, length) |
| Parameters | string = String, startIndex = Integer, length = Integer |
| Returns | String 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.
| Syntax | find(string, subString) |
| Parameters | string = String, subString = String |
| Returns | Integer or invalid |
| Examples: | var a = String.find("foobar", "bar"); // a = 3var b = String.find("foobar", "kuku"); // b = -1var c = String.find("foobar", ""); // c = invalidvar 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.
| Syntax | replace(string, oldString, newString) |
| Parameters | string = String, oldString = String, newString = String |
| Returns | String 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.
| Syntax | elements(string, separator) |
| Parameters | string = String, separator = String |
| Returns | Integer or invalid |
| Examples: | var a = String.elements("Doc JavaScript is a leading source for JavaScript tips", " "); // a = 9var b = String.elements("Doc JavaScript is a leading source for JavaScript tips", "J"); // b = 2var 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.
| Syntax | elementAt(string, index, separator) |
| Parameters | string = String, index = Integer, separator = String |
| Returns | String 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 = invalidvar f = String.elementAt("Doc JavaScript is a leading source for JavaScript tips", "kuku", " "); // f = invalidvar 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
Produced by Yehuda Shiran and Tomer Shiran
Created: June 5, 2000
Revised: June 5, 2000
URL: http://www.webreference.com/js/column63/5.html