You are reading the article Complete Guide To Typescript String updated in September 2023 on the website Lifecanntwaitvn.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 Complete Guide To Typescript String
Introduction to TypeScript StringA sequence of character values is represented by an object called a string in TypeScript which is used to store text data and is a primitive data type consisting of several helper methods and these string values are enclosed in either single quotation marks or double quotation marks and a string is same as an array of characters and there are several methods associated with a string object namely valueOf(), toUpperCase(), toString(), toLowerCase(), toLocaleUpperCase(), toLocaleLowerCase(), substring(), substr(), split(), slice(), search(), replace(), match(), localeCompare(), lastIndexOf(), indexOf(), concat(), charCodeAt(), charAt() and there are three properties associated with a string object namely Constructor, Length and Prototype.
Start Your Free Software Development Course
Syntax to Declare a String Object in TypeScript:
var variable_name = new String("string_to_be_stored");Where variable_name is the name of the variable in which the string string_to_be_stored is stored.
Steps to Define a String in TypeScript
A string in TypeScript is a sequence of characters represented by an object.
A string in TypeScript is used to store text data and is a primitive data type consisting of several helper methods.
A string in TypeScript can be defined using a constructor called String() using the new keyword.
The String() constructor returns a reference to the created string object.
Steps to Assign a Value to a String in TypeScript
A string in TypeScript can be defined using a constructor called String() using the new keyword.
The value to be assigned to the string can be passed as a parameter to the String() constructor.
The value to be assigned to the string passed as a parameter to the String() constructor must be enclosed in single quotation marks or double quotation marks.
The reference returned by the String() constructor referring to the created string object points to the string value stored in the string object.
Examples of TypeScript StringGiven below are the examples of TypeScript String:
Example #1TypeScript program defines a string using String() constructor and assigns a value to it and then finds the length of the assigned value and displays it as the output on the screen.
Code:
var newstring = new String("Shobha"); console.log("The string value defined using String() constructor is:n"); console.log(newstring); var stringlength = newstring.length; console.log("The length of the string is:n", stringlength);Output:
Example #2TypeScript program defines two strings using String() constructor and assign values to them and then concatenate the two strings using the concat() method and display the resulting string as the output on the screen.
Code:
var newstring1 = new String("Shobha"); var newstring2 = "Shivakumar"; var fullname = newstring1.concat(newstring2); console.log("The string value after concatenating the two strings is:n"); console.log(fullname);Output:
In the above program, we are defining a string using the String() constructor. Then we are assigning a value to the defined string and storing it in a variable called newstring1. Then we are defining another string and storing it in another variable called newstring2. Then we are making use of the concat() method to concatenate the two strings newstring1 and newstring2, to a single string stored in a variable called fullname. The resulting string from concatenation is displayed as the output on the screen. The output is shown in the snapshot above.
Example #3Code:
var newstring1 = new String("shobha"); var newstring2 = "shivakumar"; var fullname = newstring1.concat(newstring2); console.log("The string value after concatenating the two strings is:n"); console.log(fullname); console.log("n"); console.log("The resulting string after converting to upper case letters is:n", fullname.toUpperCase());Output:
In the above program, we are defining a string using the String() constructor. Then we are assigning a value to the defined string and storing it in a variable called newstring1. Then we are defining another string and storing it in another variable called newstring2. Then we are making use of the concat() method to concatenate the two strings newstring1 and newstring2, to a single string stored in a variable called fullname. We are then using the toUpperCase() method to convert the resulting string to upper case letters, and the resulting string is displayed as the output on the screen. The output is shown in the snapshot above.
Rules and RegulationsGiven below are the rules and regulations for working with strings in TypeScript:
A string is made up of a sequence of character values only.
A string can be used to store text data only.
The values can be assigned to a string only by enclosing them in single quotation marks or double quotation marks.
A string can be defined using a String() constructor or by enclosing the value to be stored as a string in single quotation marks or double quotation marks.
Recommended Articles
We hope that this EDUCBA information on “TypeScript String” was beneficial to you. You can view EDUCBA’s recommended articles for more information.
You're reading Complete Guide To Typescript String
Update the detailed information about Complete Guide To Typescript String on the Lifecanntwaitvn.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!