String in TypeScript
The String primitive type corresponds to the similarly named JavaScript primitive type and represents a sequences 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
class Myclass {
Myfunction() {
var name: string = "Dinesh";//string of characters
var age: string = "30"; //string made of an integer
document.write("Author Name=" + name + " <b>And</b> Author Age="+age);
}
}
window.onload = () =>{
var data = new Myclass();
data.Myfunction(); } |
Output
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.
class Myclass {
Myfunction() {
var firstname: string = "Dinesh";//string of characters
var lastname: string = "Binewal";
var age: string = "30";
document.write(firstname + " " + lastname + " is " + age + " years old.");
}
}
window.onload = () =>{
var data = new Myclass();
data.Myfunction(); } |
Output
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.
class Myclass {
Myfunction() {
var info: string = "Dinesh Binewal is 30 years old.";//string of characters
var startposition: number = info.indexOf("is") + 1;
document.write("starting position of <b>is</b> = " + startposition);
var age: string = info.substring(startposition + 4,17);
document.write(" <b>and</b> Age=" + age);
}
}
window.onload = () =>{
var data = new Myclass();
data.Myfunction(); } |
Output
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.
class Myclass {
Myfunction() {
var name: string = "Dinesh";
var age: number = 30;//numver type
document.write("Name=" + name + " <b>and</b> age=" + age.toString());
}
}
window.onload = () =>{
var data = new Myclass();
data.Myfunction(); } |
Output
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:
class Myclass {
Myfunction() {
var name: string = "Dinesh";
document.write("Name= ");
for (var i = 0; i <= name.length - 1; i++) {
document.write("" + name[i]);
}
}
}
window.onload = () =>{
var data = new Myclass();
data.Myfunction(); } |
Output
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:
class Myclass {
Myfunction() {
var name: string = "Dinesh Beniwal";
document.write("Length of string is= " + name.length);
}
}
window.onload = () =>{
var data = new Myclass();
data.Myfunction(); } |
Output
Empty string
It contains zero (0) characters. Here I am defining two ways to create an empty string. The code is given below:
class Myclass {
Myfunction() {
var empty = "";
var name: string = "";
if (name != empty) {
document.write("" + name);
}
else {
document.write("Start" + empty + "End" + " <b>Again</b> Start" + name + "End");
}
}
}
window.onload = () =>{
var data = new Myclass();
data.Myfunction(); } |
Output
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:
class Myclass {
Myfunction() {
var nullstring = null;
var empty = "";
var name:string="Dinesh"
if (name != nullstring && (name!=empty)) {
document.write("Name " +name+" is neither null or nor empty");
}
else {
document.write("name may be null or empty or both");
}
}
}
window.onload = () =>{
var data = new Myclass();
data.Myfunction(); } |
Output