EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials TypeScript Tutorial TypeScript String
Secondary Sidebar
TypeScript Tutorial
  • TypeScript Basic and Advanced
    • What is TypeScript?
    • Typescript Examples
    • TypeScript Versions
    • TypeScript Operators
    • JavaScript dump object
    • JavaScript get Method
    • Webpack ReactJS
    • Code Generator JavaScript
    • JavaScript Projects
    • Call Stack JavaScript
    • JavaScript Projects GitHub
    • JavaScript Filter Function
    • JavaScript nan
    • JavaScripttimestamp
    • TypeScript loop
    • CoffeeScript
    • TypeScript Webpack
    • setTimeout TypeScript
    • DHTMLX
    • CoffeeScript for loop
    • TypeScript number
    • JavaScript export module
    • TypeScript string contains
    • TypeScript Inheritance
    • TypeScript get
    • TypeScript undefined
    • TypeScript Global Variable
    • TypeScript Dictionary
    • TypeScript Generic
    • TypeScript Cast Object
    • TypeScript Optional Parameters
    • TypeScript? switch
    • TypeScript promise
    • TypeScript tuple
    • TypeScript Hashmap
    • TypeScript let
    • TypeScript Getter
    • TypeScript Pattern Matching
    • TypeScript number to string
    • TypeScript substring
    • TypeScript?lambda
    • TypeScript UUID
    • TypeScript JSDoc
    • TypeScript Decorators
    • Typescript for loop
    • TypeScript HTTP Request
    • TypeScript Abstract Class
    • TypeScript Question Mark
    • TypeScript Nullable
    • TypeScript reduce
    • TypeScript Mixins
    • TypeScript keyof
    • TypeScript string to number
    • TypeScript JSON parse
    • TypeScript const
    • TypeScript declare module
    • TypeScript String
    • TypeScript filter
    • TypeScript Multiple Constructors
    • TypeScript? Set
    • TypeScript string interpolation
    • TypeScript instanceof
    • TypeScript JSON
    • TypeScript Arrow Function
    • TypeScript generator
    • TypeScript namespace
    • TypeScript default parameter
    • TypeScript cast
    • TypeScript babel
    • Typescript Key-Value Pair
    • TypeScript if
    • TypeScript keyof Enum
    • TypeScript wait
    • TypeScript Optional Chaining
    • TypeScript JSX
    • TypeScript Version Check
    • TypeScript Unit Testing
    • TypeScript Handbook
    • TypeScript module
    • TypeScript Extend Interface
    • TypeScript npm
    • TypeScript pick
    • TypeScript Interface Default Value
    • JavaScript import module
    • Obfuscate Javascript
    • TypeScript basics
    • setInterval TypeScript
  • Type of Union
    • TypeScript Object Type
    • TypeScript type check
    • TypeScript promise type
    • TypeScript JSON type
    • TypeScript Union Types
    • TypeScript typeof
    • TypeScript Types
  • TypeScript Array
    • TypeScript Array of Objects
    • Methods TypeScript Array
    • TypeScript remove item from array
    • TypeScript add to array
    • TypeScript Array Contains
  • Function Of Array
    • TypeScript Function Interface
    • TypeScript Functions
    • TypeScript Export Function
    • TypeScript function return type

TypeScript String

TypeScript String

Introduction to TypeScript String

A 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.

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.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

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 String

Given below are the examples of TypeScript String:

Example #1

TypeScript 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:

//defining a string using String() constructor and assigning a value "Shobha" to it to display as the output on the screen
var newstring = new String("Shobha");
console.log("The string value defined using String() constructor is:\n");
console.log(newstring);
//using length property to find the length of the string and displaying it as the output on the screen
var stringlength = newstring.length;
console.log("The length of the string is:\n", stringlength);

Output:

TypeScript String 1

In the above program, we are defining a string using the String() constructor. Then we are assigning a value to the defined string, which is displayed as the output on the screen. Then we are finding the length of the string by using the length property, which is then displayed as the output on the screen. The output is shown in the snapshot above.

Example #2

TypeScript 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:

//defining a string using String() constructor and storing it in a variable called newstring1
var newstring1 = new String("Shobha");
//defining another string and storing it in another variable called newstring2
var newstring2 = "Shivakumar";
//using concat() method to concatenate the two strings and display the resulting string as the output on the screen
var fullname = newstring1.concat(newstring2);
console.log("The string value after concatenating the two strings is:\n");
console.log(fullname);

Output:

TypeScript String 2

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 #3

TypeScript program to define two strings using String() constructor and assign values to them and then concatenate the two strings using concat() method and then convert the resulting string to upper case letters using toUppercase() method and display the resulting string as the output on the screen.

Code:

//defining a string using String() constructor and storing it in a variable called newstring1
var newstring1 = new String("shobha");
//defining another string and storing it in another variable called newstring2
var newstring2 = "shivakumar";
//using concat() method to concatenate the two strings and display the resulting string as the output on the screen
var fullname = newstring1.concat(newstring2);
console.log("The string value after concatenating the two strings is:\n");
console.log(fullname);
console.log("\n");
//using toUpperCase() function to convert the given string to upper case letters
console.log("The resulting string after converting to upper case letters is:\n", fullname.toUpperCase());

Output:

concat() method

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 Regulations

Given 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

This is a guide to TypeScript String. Here we discuss the introduction, steps to define and assign a value to a string in TypeScript and examples. You may also have a look at the following articles to learn more –

  1. TypeScript let
  2. TypeScript Optional Parameters
  3. TypeScript Generic
  4. What is TypeScript?
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

C# Programming, Conditional Constructs, Loops, Arrays, OOPS Concept

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more