Result | String function | Use / Result |
String | string.charAt(position) | get character at position in string |
Example: | str.charAt(2) | |
Number | string.charCodeAt(position) | get character code at position in string |
Example: | str.charCodeAt(2) | |
String | string.concat(string1[, ... stringN]) | append one or more Strings |
Example: | str.concat(" ", "Right on!") | |
Number | string.indexOf(searchString[, position]) | find position of searchString within string; you can also search for a regular expression using String.search(/regexp/flags) |
Examples: | str.indexOf("with") | |
str.indexOf("with", 16) | |
Number | string.lastIndexOf(searchString[, position]) | find last position of searchString within string |
Examples: | str.lastindexOf("with") | |
str.lastindexOf("with", 31) | |
Number | string.localeCompare(string1, string2) | locale-sensitive comparison of one string with another string
Note that:
- comparison is dependent on the client system's locale, not the server-side system's locale, and
- although a case-sensitive comparision is used, which letter in the alphabet is considered a higher priority for determining order than its case - in English, for example, lower case comes before upper case, so the order of alphabetic characters is "a", "A", "b", "B", etc. (see examples below)
|
Example: | "A".localeCompare("a") | |
"A".localeCompare("b") | |
"a".localeCompare("B") | |
Array | string.match(/regexp/[flags]) | retrieve substrings of a String that match a given regular expression |
Example: | str.match(/with/gi) | |
String | string.replace(searchString, replacement) string.replace(/regexp/flags, replacement) | replace part of one string with another string |
Example: | str.replace("start", "begin") | |
Number | string.search(/regexp/[flags]) | get index of a substring that matches a regular expression; to match a string instead use String.indexOf(substring[, position]) |
Example: | str.search(/[^a-z\s]/i) | |
String | string.slice(start, end) | get a substring of a string |
Example: | str.slice(16, 31) | |
Array | string.split(separator[, limit]) string.split(/regexp/[, limit]) | split String based on separator |
Examples: | str.split(" ")[2] // third word | |
str.split(/\s/)[3] // fourth word | |
"first " + str.split(" ", 5).length + " of " + str.split(" ").length | |
String | string.toLocaleLowerCase() | convert string to lower case based on client locale |
Example: | str.toLocaleLowerCase() | |
String | string.toLocaleUpperCase() | convert string to upper case based on client locale |
Example: | str.toLocaleUpperCase() | |
String | string.toLowerCase() | convert string to lower case |
Example: | str.toLowerCase() | |
String | string.toString() | the String itself, same as String.valueOf() |
Example: | str.toString() | |
String | string.toUpperCase() | convert string to upper case |
Example: | str.toUpperCase() | |
String | string.trim() | remove leading and trailing spaces |
Example: | " TEST ".trim() | |
String | string.valueOf() | the String itself, same as String.toString() |
Example: | str.valueOf() | |
Function / Operator | Use / Result |
String | decodeURI(encodedURI) | decode an encoded URI |
Examples: | decodeURI("/The%20Firm/") | |
String | decodeURIComponent(encodedComponent) | decode a query parameter or other encoded URI component |
Examples: | decodeURIComponent("decimal%E2%80%94To%E2%80%94%25") + "=" + decodeURIComponent("pct%20%3D%20dec%23%20*%20100") | |
String | encodeURI(unencodedURI) | encode a URI |
Examples: | document.createElement("a").href = encodeURI("http://TVSeries.com/The Firm/"); | |
String | encodeURIComponent(unencodedComponent) | encode a query string parameter or other URI component |
Examples: | encodeURIComponent("decimal—To—%") + "=" + encodeURIComponent("pct = dec# * 100") | |
String | escape(string) | deprecated escape all special characters in a string |
Examples: | escape("decimal—To—%") + "=" + escape("pct = dec# * 100") | |
(any) | eval(string1[, ... stringN]) | evaluate string as a JavaScript expression, returning the value of the last expression |
Examples: | eval(".67 * 100") + "%" | |
Number | parseFloat(string) | convert String to a floating point Number |
Examples: | var intval = parseFloat("123.45"); | |
Number | parseInt(string) | convert String to an integer Number |
Examples: | var intval = parseInt("123"); | |