Easy Tutorial
❮ Ts Type Ts Loop ❯

TypeScript String

The String object is used to manipulate text (strings).

Syntax

var txt = new String("string");
Or more simply:
var txt = "string";

String Object Properties

The following table lists the properties supported by the String object:

No. Property & Description Example
1. constructor: A reference to the function that created the object. var str = new String("This is string"); <br>console.log("str.constructor is:" + str.constructor); // Output: str.constructor is: function String() { [native code] }
2. length: Returns the length of the string. var uname = new String("Hello World"); <br>console.log("Length " + uname.length); // Output: 11
3. prototype: Allows you to add properties and methods to the object. function employee(id: number, name: string) { <br> this.id = id; <br> this.name = name; <br>} <br>var emp = new employee(123, "admin"); <br>employee.prototype.email = "[email protected]"; // Add property email <br>console.log("Employee ID: " + emp.id); <br>console.log("Employee Name: " + emp.name); <br>console.log("Employee Email: " + emp.email);

String Methods

No. Method & Description Example
1. charAt(): Returns the character at the specified position. var str = new String("tutorialpro"); <br>console.log("str.charAt(0) is:" + str.charAt(0)); // R <br>console.log("str.charAt(1) is:" + str.charAt(1)); // U <br>console.log("str.charAt(2) is:" + str.charAt(2)); // N <br>console.log("str.charAt(3) is:" + str.charAt(3)); // O <br>console.log("str.charAt(4) is:" + str.charAt(4)); // O <br>console.log("str.charAt(5) is:" + str.charAt(5)); // B
2. charCodeAt(): Returns the Unicode of the character at the specified position. var str = new String("tutorialpro"); <br>console.log("str.charCodeAt(0) is:" + str.charCodeAt(0)); // 82 <br>console.log("str.charCodeAt(1) is:" + str.charCodeAt(1)); // 85 <br>console.log("str.charCodeAt(2) is:" + str.charCodeAt(2)); // 78 <br>console.log("str.charCodeAt(3) is:" + str.charCodeAt(3)); // 79 <br>console.log("str.charCodeAt(4) is:" + str.charCodeAt(4)); // 79 <br>console.log("str.charCodeAt(5) is:" + str.charCodeAt(5)); // 66
3. concat(): Combines two or more strings and returns a new string. var str1 = new String("tutorialpro"); <br>var str2 = new String("GOOGLE"); <br>var str3 = str1.concat(str2); <br>console.log("str1 + str2 : " + str3); // tutorialproGOOGLE
4. indexOf(): Returns the position of the first occurrence of a specified value in a string. var str1 = new String("tutorialpro"); <br>var index = str1.indexOf("OO"); <br>console.log("Index of the string found : " + index); // 3
5. lastIndexOf(): Searches the string from the end to the beginning and returns the position of the last occurrence of a specified value. var str1 = new String("This is string one and again string"); <br>var index = str1.lastIndexOf("string"); <br>console.log("LastIndexOf found the last string position : " + index); // 29 <br>index = str1.lastIndexOf("one"); <br>console.log("LastIndexOf found the last string position : " + index); // 15
6. localeCompare(): Compares two strings in the current locale. var str1 = new String("This is beautiful string"); <br>var index = str1.localeCompare("This is beautiful string"); <br>console.log("localeCompare first : " + index); // 0
7. match(): Searches a string for a match against a regular expression, and returns the matches. var str = "The rain in SPAIN stays mainly in the plain"; <br>var n = str.match(/ain/g); // ain,ain,ain
8. replace(): Replaces a specified value with another value in a string. var re = /(\w+)\s(\w+)/; <br>var str = "zara ali"; <br>var newstr = str.replace(re, "$2, $1"); <br>console.log(newstr); // ali, zara
9. search(): Searches a string for a specified value, or regular expression, and returns the position of the match. var re = /apples/gi; <br>var str = "Apples are round, and apples are juicy."; <br>if (str.search(re) == -1) { <br> console.log("Does not contain Apples"); <br>} else { <br> console.log("Contains Apples"); <br>}
10. slice(): Extracts a part of a string and returns a new string.
11. split(): Splits a string into an array of substrings. var str = "Apples are round, and apples are juicy."; <br>var splitted = str.split(" ", 3); <br>console.log(splitted); // [ 'Apples', 'are', 'round,' ]
12. substr(): Extracts a specified number of characters in a string, from a start index.
13. substring(): Extracts characters from a string between two specified indices. var str = "tutorialpro GOOGLE TAOBAO FACEBOOK"; <br>console.log("(1,2): " + str.substring(1,2)); // U <br>console.log("(0,10): " + str.substring(0, 10)); // tutorialpro GOO <br>console.log("(5): " + str.substring(5)); // B GOOGLE TAOBAO FACEBOOK
14. toLocaleLowerCase(): Converts a string to lowercase according to the host's locale. var str = "tutorialpro Google"; <br>console.log(str.toLocaleLowerCase()); // tutorialpro google
15. toLocaleUpperCase(): Converts a string to uppercase according to the host's locale. var str = "tutorialpro Google"; <br>console.log(str.toLocaleUpperCase()); // tutorialpro GOOGLE
16. toLowerCase(): Converts a string to lowercase. var str = "tutorialpro Google"; <br>console.log(str.toLowerCase()); // tutorialpro google
17. toString(): Returns the string. var str = "tutorialpro"; <br>console.log(str.toString()); // tutorialpro
18. toUpperCase(): Converts a string to uppercase. var str = "tutorialpro Google"; <br>console.log(str.toUpperCase()); // tutorialpro GOOGLE
19. valueOf(): Returns the primitive value of a String object. var str = new String("tutorialpro"); <br>console.log(str.valueOf()); // tutorialpro
❮ Ts Type Ts Loop ❯