String in TypeScript
String of characters
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";
- var age: string = "30";
- document.write("Author Name=" + name + " <b>And</b> Author Age=" + age);
- }
- }
- window.onload = () => {
- var data = new Myclass();
- data.Myfunction();
- }
Output
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.

Create a string using concatenation
- class Myclass {
- Myfunction() {
- var firstname: string = "Dinesh";
- 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
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.

Create a sting using a property or a method
- class Myclass {
- Myfunction() {
- var info: string = "Dinesh Binewal is 30 years old.";
- 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
The toString method returns a string. We can apply the toString method on any data type, which is converted to a string.

Create a string using the toString Method
- class Myclass {
- Myfunction() {
- var name: string = "Dinesh";
- var age: number = 30;
- 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
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:

Size of 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
It contains zero (0) characters. Here I am defining two ways to create an empty string. The code is given below:

Empty string
- 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
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:

Null string
- 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


Manish SharmaPosted Dec 14, 2012, 5:57 AM
Nice article. It is very helpful to understand the use of String in TypeScript...