Typescript String Tutorial

A string is a collection of characters, that wraps the string primitive data type with a number of helper methods. We are going to discuss the method and helper of typescript below,

String Object’s method

There is 3 string object method available and they are:

  1. Constructor,
  2. Length,
  3. Prototype

1. Constructor:

The constructor returns a reference to the String function that created the object.

Constructor Example:
var strng = new String( "This is a sample string" ); 
console.log("the constructor is:" + strng.constructor);

2. Length:

The length returns the total length or number of the characters including the whitespace.

Length Example:
var strnm = new String("My first string");
console.log(strnm);
console.log("Length of the string is: "+strnm.length);

3. Prototype:

The prototype allows adding properties and methods to the object.

Prototype Example:

function student(id:number,name:string) { 
   this.id = id;
   this.name = name;
} 
var stdnt = new student(123,"Smith"); 
student.prototype.email="[email protected]";
console.log("Id of the student is: "+stdnt.id);
console.log("Name of the student is: "+stdnt.name);
console.log("Email address of the student is: "+stdnt.email);

String Methods

There is a lot of method available in string object and they are as described below,

  1. charAt(),
  2. charCodeAt(),
  3. concat(),
  4. indexOf(),
  5. lastIndexOf(),
  6. localeCompare(),
  7. match(),
  8. replace(),
  9. search(),
  10. slice(),
  11. split(),
  12. substr(),
  13. substring(),
  14. toLocaleLowerCase(),
  15. toLocaleUpperCase(),
  16. toLowerCase(),
  17. toString(),
  18. toUpperCase(),
  19. valueOf()

charAt(): charAt is a method to return the character of the nth position, the position of the character is starting from left to right and starts from 0 and follows towards the right as 0,1,2,3…. and so on where the last character of the string can be got by passing the position minus 1 (-1).

Example of charAt():
var strng = new String("This is a sample string"); 
console.log( "char at position 0 is: " + strng.charAt(0) ); 
console.log( "char at position 1 is: " + strng.charAt(1) ); 
console.log( "char at position 2 is: " + strng.charAt(2) ); 
console.log( "char at position 3 is: " + strng.charAt(3) ); 
console.log( "char at position 4 is: " + strng.charAt(4) ); 
console.log( "char at position 5 is: " + strng.charAt(5) );

charCodeAt(): similar to the carAt method we have to pass a position of the string but the different is it will return the character code of the character located at the position, charCodeAt() methods position also starts with 0, and follows the left to right like 0,1,2…and so on and the last character code can be got by passing minus one (-1).

Example of charCodeAt():
var strng = new String("This is a sample string"); 
console.log( "char  code at 0 is: " + strng.charCodeAt(0) ); 
console.log( "char  code at 1 is: " + strng.charCodeAt(1) ); 
console.log( "char  code at 2 is: " + strng.charCodeAt(2) ); 
console.log( "char  code at 3 is: " + strng.charCodeAt(3) ); 
console.log( "char  code at 4 is: " + strng.charCodeAt(4) ); 
console.log( "char  code at 5 is: " + strng.charCodeAt(5) );

concat(): The 3. concat() method is used to make concatenations between two strings.

Example of concat():
var strng1 = new String( "This is the sample string one" ); 
var strng2 = new String( "This is the sample string two" ); 
var strng3 = strng1.concat( strng2 ); 
console.log("strng1 + strng2 : "+strng3)

indexOf(): in the indexOf method, we have to pass a string and its uses to detect the starting character position of the first character if found in the string.

Example of indexOf():
var strng1 = new String( "This is the sample string one" ); 
 
var indexof = strng1.indexOf( "string" ); 
console.log("index of found string is : " + indexof );   

var indexof2 = strng1.indexOf( "one" ); 
console.log("index of found string is : " + indexof2 );

lastIndexOf(): In the lastIndexOf method the position of the last character in a string if found in the string, here we have to pass a string also inside the method like the method indexOf.

lastIndexOf() Example:
var strng = new String( "This is the sample string one and another sample string" ); 
var lastindexof = strng.lastIndexOf( "string" );
console.log("last Index of found String is : " + lastindexof ); 
  
lastindexof2 = strng.lastIndexOf( "one" ); 
console.log("last index of found String is : " + lastindexof2 );

localeCompare(): The localeCompare method is used to check if a string is matched or partially matched with a string. it will return 0 if the string is fully matched and will return 1 if the string is not matched.

Example of localeCompare():
var strng = new String( "This is a sample string" );  
var indexoflocalcompair = strng.localeCompare( "This is a sample string");  
console.log("localeCompare first :" + indexoflocalcompair );

match(): the match() method is used to match with some regular expressions.

Example of the match():
var regular_expression = /(\w+)\s(\w+)/; 
var strng = "Code Mystery"; 
var reslt = strng.match(regular_expression); 
console.log("The matched content is: " + reslt);

replace(): the method replace() is used to find a match between a regular expression and a string and replace it with some other string that matched the target string

Example of replace():
var regular_expression = /World/s; 
var strng = "Hellow World.";
var newstrng = strng.replace(regular_expression, "Earth"); 
console.log(newstrng)

search(): the search method is used to search for the existence of a regular expression in a string. It returns -1 if not found the match is.

Example of search():

var regular_expression = /World/s; 
var strng = "Hellow World.";
if (strng.search(regular_expression) == -1 ) { 
   console.log("Does not exist contain World" ); 
} else { 
   console.log("Exist contain World" ); 
} 

slice(): The slice() method extract a slice of string from the string, Here we have to pass two numerical value the first one is from where the slice will start and the second one is up to the position of character the slice will be done.

Example of slice():
var strng = "Hellow World."; 
var sliced_strng = strng.slice(2, -3); 
console.log(sliced_strng);

split(): The method split() is used to split a string into several pieces and store it into an array by a character or string, here also can be sent two parameters the first one is in which character or string the specification will be done called a separator, and the second one is the number of array value to store from the beginning.

Example of split():
var strng = "Hellow World is the first string for coding.";
var splitted_strng = strng.split(" ", 3); 
console.log(splitted_strng)

substr(): the substr() method returns a string subtracting from the target string, here have to pass two parameters, the first one is from where the substruction will be done, and the second one for till the character position the substruction will be done.

Example of substr():
var strng = "Hellow World is the first string for coding.";
console.log("substr of (4,12): "    + strng.substr(4,12)); 
console.log("substr of (-3,5): "   + strng.substr(-3,5)); 
console.log("substr of (3): "      + strng.substr(3)); 
console.log("substr of (-15,3): " + strng.substr(-15,3)); 
console.log("substr of (15,3): "  + strng.substr(15,3));

substring(): the method substring() is also used to take a string from a string between two characters index like substr method.

Example of substring():
var strng = "Hellow World is the first string for coding.";
console.log("substring of (4,12): "    + strng.substring(4,12)); 
console.log("substring of (-3,5): "   + strng.substring(-3,5)); 
console.log("substring of (3): "      + strng.substring(3)); 
console.log("substring of (-15,3): " + strng.substring(-15,3)); 
console.log("substring of (15,3): "  + strng.substring(15,3));

toLocaleLowerCase(): on respect of current position thwe method toLocaleLowerCase() convert the string respecting the current locale in lower case.

example of toLocaleLowerCase( ):
var strng = "Hellow World.";
console.log(strng.toLocaleLowerCase( ));

toLocaleUpperCase(): on respect of current position thwe method toLocaleUpperCase() convert the string respecting the current locale in upper case.

Example of toLocaleUpperCase():
var strng = "Hellow World."; 
console.log(strng.toLocaleUpperCase( ));

toLowerCase(): the method toLowerCase() converts a string to lower case.

Example of toLowerCase():
var strng = "Hellow World."; 
console.log(strng.toLowerCase( ));

toString(): the method toString() is used to convert a specific object to a string value. It could be said to type casting to stringing from an object.

Example of toString():
var strng = "Hellow World."; 
console.log(strng.toString( ));

toUpperCase(): The method is used to make a string in the upper case.

Example of toUpperCase():
var strng = "Hellow World.";  
console.log(strng.toUpperCase( ));

valueOf( ): the method returns the premitive value of a string object.

Example of valueOf( ):
var strng = new String("Hello world"); 
console.log(strng.valueOf( ));

We have discussed a number of objects and methods of strings that we can use with string objects.

Share The Post On -