String:




String is a global object that may be used to construct String instances.

String objects may be created by calling the constructor new String() The String object wraps JavaScript's string primitive data type with the methods described below. The global function String can also be called without new in front to create a primitive string. String literals in JavaScript are primitive strings.

Because JavaScript automatically converts between string primitives and String objects, you can call any of the methods of the String object on a string primitive. JavaScript automatically converts the string primitive to a temporary String object, calls the method, then discards the temporary String object. For example, you can use thString.length property on a string primitive created from a string literal.




Example:

s_obj = new String(s_prim = s_also_prim = "foo");

s_obj.length; // 3
s_prim.length; // 3
s_also_prim.length; // 3
'foo'.length; // 3
"foo".length; // 3
console.log('s_obj.length:',s_obj.length);
console.log('s_prim.length:',s_prim.length);
console.log('s_also_prim.length:',s_also_prim.length);
console.log('foo with single quotes:','foo'.length);
console.log('foo with double quotes:',"foo".length);
OUTPUT:




s_obj.length: 3
s_prim.length: 3
s_also_prim.length: 3
foo with single quotes: 3
foo with double quotes: 3



String primitives and String objects give different results when evaluated as JavaScript. Primitives are treated as source code; String objects are treated as a character sequence object. For example:

s1 = "2 + 2"; // creates a string primitive
s2 = new String("2 + 2"); // creates a String object
eval(s1); // returns the number 4
eval(s2); // returns the string "2 + 2"
eval(s2.valueOf()); // returns the number 4



Functions:

charAt( index ) : String
Returns the character at the specified index.
example :
console.log('cat'.charAt(2));
OUTPUT:

t



concat( strings ) : String


Combines combines the text from one or more strings and returns a new string. Changes to the text in one string do not affect the other string.
var hello = "Hello, ";
console.log(hello.concat("Kevin", " have a nice day."));
OUTPUT:

Hello, Kevin have a nice day.


indexOf( searchValue, fromIndex ) : Number
Returns the index within the calling String object of the first occurrence of the specified value, or -1 if not found.



console.log("Sencha Touch".indexOf("T"));
OUTPUT:

7


lastIndexOf( searchValue, fromIndex ) : Number


Returns the index within the calling String object of the last occurrence of the specified value, or -1 if not found. The calling string is searched backward, starting at fromIndex.
console.log("radar".lastIndexOf("a"));
OUTPUT:

3



localeCompare( compareString ) : Number


Returns a number indicating whether a reference string comes before or after or is the same as the given string in sort order.



console.log('a'.localeCompare('b')); // -1
console.log('b'.localeCompare('a')); // 1
console.log('b'.localeCompare('b')); // 0



split( seperator, limit ) : Array


Splits a String object into an array of strings by separating the string into substrings.

var myString = "Welcome to the sencha world";
var splits = myString.split(" ", 3);
console.log(splits);
OUTPUT:

["Welcome", "to", "the"]