TypeScript tutorial: Lesson 8 – String


Example about String:

var str2 = `<html><body>
    <h1>Tutorialspots.com</h1>
</body></html>`  //multiple line
var str3 = 'Tutorialspots.com' //single line
var str4 = "Tutorialspots.com" //single line
var str4 = `My website is ${str3}` //with variables

The String object

Syntax

var my_string = new String(string)

String Properties

constructor : Returns a reference to the String function that created the object.

Example:

var my_string = new String("tutorialspots"); 
console.log("my_string.constructor is:" + my_string.constructor)

Result:

[LOG]: my_string.constructor is:function String() { [native code] } 

length : Returns the length of the string.

Example:

var my_string = new String("tutorialspots"); 
console.log("my_string.length is:" + my_string.length)

Result:

[LOG]: my_string.length is:13 

String Methods:

S.No. Method & Description
1. charAt()

Returns the character at the specified index.

Syntax

my_string.charAt(index)

Arguments:
index − An integer between 0 and 1 less than the length of the string.

Example:

var my_string = new String("tutorialspots"); 
console.log("my_string.charAt(0) is:" + my_string.charAt(0)); 
console.log("my_string.charAt(1) is:" + my_string.charAt(1)); 

Result:

[LOG]: my_string.charAt(0) is:t 
[LOG]: my_string.charAt(1) is:u 
2. charCodeAt()

Returns a number indicating the Unicode value of the character at the given index.

Syntax

my_string.charCodeAt(index)

Arguments:
index − An integer between 0 and 1 less than the length of the string; if unspecified, defaults to 0.

Example:

var my_string = new String("tutorialspots"); 
console.log("my_string.charCodeAt(0) is:" + my_string.charCodeAt(0)); 
console.log("my_string.charCodeAt(1) is:" + my_string.charCodeAt(1)); 

Result:

[LOG]: my_string.charCodeAt(0) is:116 
[LOG]: my_string.charCodeAt(1) is:117 
3. concat()

Combines the text of two strings and returns a new string.

Syntax

my_string.concat(my_string2, my_string3[, ..., my_stringN])

Arguments:
my_string2,...,my_stringN − These are the strings to be concatenated.

Example:

var my_string1 = new String("tutorialspots")
var my_string2 = new String(".com")
console.log("my_string1 + my_string2 is:" + my_string1.concat(my_string2.toString()))

Result:

[LOG]: my_string1 + my_string2 is:tutorialspots.com 
4. indexOf()

Returns the index within the calling String object of the first occurrence of the specified value, or -1 if not found.

5. lastIndexOf()

Returns the index within the calling String object of the last occurrence of the specified value, or -1 if not found.

6. localeCompare()

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

Syntax

my_string.localeCompare( my_string2 )

Arguments:
my_string2 − A string to be compared with string object.

Return Value:

  • 0 − If the string matches 100%.
  • 1 − no match, and the parameter value comes before the string object’s value in the locale sort order.
  • A negative value − no match, and the parameter value comes after the string object’s value in the local sort order.

Example:

var my_string1 = new String("tutorialspots")
var my_string2 = new String("tutorialspots")
var my_string3 = new String("an useful website")
var my_string4 = new String("very good")
console.log("localeCompare from my_string1 to my_string2 is:" + 
    my_string1.localeCompare(my_string2.toString()))
console.log("localeCompare from my_string1 to my_string3 is:" + 
    my_string1.localeCompare(my_string3.toString()))
console.log("localeCompare from my_string1 to my_string4 is:" + 
    my_string1.localeCompare(my_string4.toString()))

Result:

[LOG]: localeCompare from my_string1 to my_string2 is:0 
[LOG]: localeCompare from my_string1 to my_string3 is:1 
[LOG]: localeCompare from my_string1 to my_string4 is:-1 
7.

match()

Used to match a regular expression against a string.

8. replace()

Used to find a match between a regular expression and a string, and to replace the matched substring with a new substring.

9. search()

Executes the search for a match between a regular expression and a specified string.

10. slice()

Extracts a section of a string and returns a new string.

11. split()

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

12. substr()

Returns the characters in a string beginning at the specified location through the specified number of characters.

13. substring()

Returns the characters in a string between two indexes into the string.

14. toLocaleLowerCase()

The characters within a string are converted to lower case while respecting the current locale.

15. toLocaleUpperCase()

The characters within a string are converted to upper case while respecting the current locale.

16. toLowerCase()

Returns the calling string value converted to lower case.

17. toString()

Returns a string representing the specified object.

18. toUpperCase()

Returns the calling string value converted to uppercase.

19. valueOf()

Returns the primitive value of the specified object.

Leave a Reply