String variables in JavaScript have a number of very useful string functions. Here's a sample of the most commonly used:
Operation / Function | Example |
|---|---|
Concatenation | var S1 = "Customer"; var S2 = "123456"; var S3 = S1 + " " + S2 + "could not be found"; |
IndexOf – finds first occurrence of a string in a string | /* 012345678901 */ var S1 = "ABCDHELLOABC"; var pos = S1.indexOf("HELLO"); |
lastIndexOf - finds last occurrence of a string in a string | /* 012345678901 */ var S1 = "ABCDHELLOABC"; var pos = S1.lastIndexOf("AB"); |
charAt – returns the character at a specific position in a string | /* 012345678901 */ var S1 = "ABCDHELLOABC"; var S2 = S1.charAt(4); var S3 = S1.charAt(9); |
length – returns the length of a string | /* 012345678901 */ var S1 = "ABCDHELLOABC"; var I = S1.length; |
substring – returns the substring of string using a starting and ending point. | /* 01234567789 */ var a = "Hello World"; var b = a.substring(4,8); |
substr – returns the substring of a string using a starting position and a length | /* 01234567789 */ var a = "Hello World"; var b = a.substr(2,3); |
toLowerCase – returns the lowercase of string | var a = "Hello World"; var b = a.toLowerCase(); |
toUpperCase – returns the uppercase of a string | var a = "Hello World"; var b = a.toUpperCase(); |
|
There are more string functions like these available. See:
{+}http://www.w3schools.com/jsref/jsref_obj_string.asp+
for more details.