String in TypeScript

String in TypeScript

 
The String primitive type corresponds to the similarly named JavaScript primitive type and represents a sequence of characters stored as Unicode UTF-16 code units. The string primitive type contains approximately twenty methods. I will not attempt an exhaustive presentation of each and every method here. To demonstrate the usability of the string methods, I have provided a few and elaborate with some examples.
 
The String primitive type represents a string. The following code creates two strings with a name and number values.
 

String of characters

 
var name: string="Dinesh";
 

String made of an integer

 
var age: string = "30";
 
This example shows how to use a string in TypeScript
  1. class Myclass {  
  2.  Myfunction() {  
  3.   var name: string = "Dinesh";  
  4.   var age: string = "30";  
  5.   document.write("Author Name=" + name + " <b>And</b>  Author Age=" + age);  
  6.  }  
  7. }  
  8. window.onload = () => {  
  9.  var data = new Myclass();  
  10.  data.Myfunction();  
  11. }  
Output
 
simple-string-in-typescript.jpg
 

Create a string using concatenation

 
The String concatenation operator (+) can be used to combine more than one string to create a single string. The following example combines two strings into a single string.
  1. class Myclass {  
  2.  Myfunction() {  
  3.   var firstname: string = "Dinesh";  
  4.   var lastname: string = "Binewal";  
  5.   var age: string = "30";  
  6.   document.write(firstname + "  " + lastname + " is " + age + " years old.");  
  7.  }  
  8. }  
  9. window.onload = () => {  
  10.  var data = new Myclass();  
  11.  data.Myfunction();  
  12. }  
Output
 
string-concatenation-in-typescript.jpg
 

Create a sting using a property or a method

 
Some properties and methods of the String primitive type returns a string object such as the substring method. In the example given we found the specified string's start position using the indexOf method and found the age within the string.
  1. class Myclass {  
  2.  Myfunction() {  
  3.   var info: string = "Dinesh Binewal is 30 years old.";  
  4.   var startposition: number = info.indexOf("is") + 1;  
  5.   document.write("starting position of <b>is</b> = " + startposition);  
  6.   var age: string = info.substring(startposition + 4, 17);  
  7.   document.write(" <b>and</b> Age=" + age);  
  8.  }  
  9. }  
  10. window.onload = () => {  
  11.  var data = new Myclass();  
  12.  data.Myfunction();  
  13. }  
Output
 
string-methods-in-typescript.jpg
 

Create a string using the toString Method

 
The toString method returns a string. We can apply the toString method on any data type, which is converted to a string.
  1. class Myclass {  
  2.  Myfunction() {  
  3.   var name: string = "Dinesh";  
  4.   var age: number = 30;  
  5.   document.write("Name=" + name + " <b>and</b> age=" + age.toString());  
  6.  }  
  7. }  
  8. window.onload = () => {  
  9.  var data = new Myclass();  
  10.  data.Myfunction();  
  11. }  
Output
 
number-to-string-in-typescript.jpg
 
Get all characters of a string using loop
 
A string is a set of characters. The following code reads all characters of the string and displays it:
  1. class Myclass {  
  2.  Myfunction() {  
  3.   var name: string = "Dinesh";  
  4.   document.write("Name= ");  
  5.   for (var i = 0; i <= name.length - 1; i++) {  
  6.    document.write("" + name[i]);  
  7.   }  
  8.  }  
  9. }  
  10. window.onload = () => {  
  11.  var data = new Myclass();  
  12.  data.Myfunction();  
  13. }  
Output
 
string-by-loop-in-typescript.jpg
 

Size of string

 
We can determine the number of characters in the string including white space using the length property. The following example shows how to use the length property with a string:
  1. class Myclass {  
  2.  Myfunction() {  
  3.   var name: string = "Dinesh Beniwal";  
  4.   document.write("Length of string is= " + name.length);  
  5.  }  
  6. }  
  7. window.onload = () => {  
  8.  var data = new Myclass();  
  9.  data.Myfunction();  
  10. }  
Output
 
string-length-property-in-typescript.jpg
 

Empty string

 
It contains zero (0) characters. Here I am defining two ways to create an empty string. The code is given below:
  1. class Myclass {  
  2.  Myfunction() {  
  3.   var empty = "";  
  4.   var name: string = "";  
  5.   if (name != empty) {  
  6.    document.write("" + name);  
  7.   } else {  
  8.    document.write("Start" + empty + "End" + " <b>Again</b> Start" + name + "End");  
  9.   }  
  10.  }  
  11. }  
  12. window.onload = () => {  
  13.  var data = new Myclass();  
  14.  data.Myfunction();  
  15. }  
Output
 
empty-string-in-typescript.jpg
 

Null string

 
A null string references the one and only value of the Null type. It is not possible to directly reference the Null type itself. If you try to use any method or property with a null string then it will fire an exception. The following code shows how to use a null string with TypeScript:
  1. class Myclass {  
  2.  Myfunction() {  
  3.   var nullstring = null;  
  4.   var empty = "";  
  5.   var name: string = "Dinesh"  
  6.   if (name != nullstring && (name != empty)) {  
  7.    document.write("Name " + name + " is neither null or nor empty");  
  8.   } else {  
  9.    document.write("name may be null or empty or both");  
  10.   }  
  11.  }  
  12. }  
  13. window.onload = () => {  
  14.  var data = new Myclass();  
  15.  data.Myfunction();  
  16. }  
Output
 
null-string-in-typescript.jpg


Similar Articles