Typescript работа со строками

TypeScript String

Strings are used to represent a sequence of Characters. Strings are immutable in TypeScript. When you perform operations on a String, TypeScript creates a new String. There are three ways in which you can create a String in TypeScript.

1. Double quoted strings: A String can be enclosed in double quotes as shown below.

let message: string = "Long long ago in a galaxy far far away"; console.log(message);

Output:
Long long ago in a galaxy far far away

2. Single quoted strings: A String can also be enclosed in single quotes as shown below.

let message: string = 'Long long ago in a galaxy far far away'; console.log(message);

Output:
Long long ago in a galaxy far far away

3. Back-ticks strings. We can write expressions using it: We can express a String inside backticks. This is useful when we have expressions inside our String. In the below example, we created an expression 2 raised to the power 3 inside backticks.

let message: string = `2 cube is $`; console.log(message);

Appending strings: We can append strings using the + operator. In the below example, we appended three Strings using plus operator.

let str1: string = "Captain"; let str2: string = "Obvious"; let result: string = str1 + " " + str2; console.log(result);

Output:
Captain Obvious

We can iterate over a TypeScript String using for..of loop. In the below example, we have a String representing vowels. We then iterated the String using for..of loop.

let vowels: string ="aeiou"; for(let vowel of vowels)

We can get the lower case by calling toLowerCase function. In the below example, we printed the String in lower case using toLowerCase function.

let letters: string = "Captain Skanda"; console.log(letters.toLowerCase());

Output:
captain skanda

We can get the upper case by calling toUpperCase function. In the below example, we printed the String in upper case using toUpperCase function.

let letters: string = "Captain Skanda"; console.log(letters.toUpperCase());

Output:
CAPTAIN SKANDA

Читайте также:  Code source application web java

We can find the length of string using length property. In the below example, we determined the length of the String using length property of the String.

let message: string = "TheQuickBrownForJumpedOverTheLazyDog"; console.log(message.length);

Using trim function we can remove leading and trailing spaces. In the below example, we trimmed the leading and trailing spaces using trim function.

let letters: string = " Captain Cosmos "; console.log(letters.trim());

Output:
Captain Cosmos

We can get character of a particular location either using index or using charAt function. In the below example, we are getting the character at position 3 using index operator as well as charAt function.

let message: string = "TheQuickBrownForJumpedOverTheLazyDog"; console.log(message[3]); console.log(message.charAt(3));

startsWith function is used to check if a given string starts with given substring. In the below example, we used startsWith function to determine if a given String starts with «Captain».

let letters: string = "Captain Cosmos"; console.log(letters.startsWith("Captain"));

endsWith function is used to check if a given string ends with given substring. In the below example, we used endsWith function to determine if a given String ends with «Cosmos».

let letters: string = "Captain Cosmos"; console.log(letters.endsWith("Cosmos"));

We can use indexOf function to search for a substring within a string in TypeScript. In the below example, we are searching for a substring inside a given string and printing the result to the log.

let letters: string = "Captain Shyam"; console.log(letters.indexOf("Shy"));

Please be informed that indexOf function returns -1 if the substring is not found as shown in the below example and output.

let letters: string = "Captain Shyam"; console.log(letters.indexOf("Shay"));

includes method returns true or false depending on if the substring is found within the string. This is useful when we don’t really want a numeric output but just a boolean to indicate if the substring is found or not.

let letters: string = "Captain Shyam"; console.log(letters.includes("Shy")); console.log(letters.includes("Shay"));

Substring — We can get substring of a given string using slice method. In the below example, we sliced a string to get a substring.

let letters: string = "Captain Cosmos"; console.log(letters.slice(0, 7)); console.log("");

Using replace function we can replace instance of substring inside a string. In the below example, we replaced a substring with another string.

let letters: string = "Captain Cosmos"; letters = letters.replace("Cosmos", "Earth"); console.log(letters);

Output:
Captain Earth

Читайте также:  Css розово черные текстуры

We can call split method on string to get array out of substrings. We need to specify to the split function the sequence which should be used to split the string. In the below example, we split the planet string on comma (,) to get the list of planets.

let planetStr: string = "Mercury,Venus,Earth,Mars,Jupiter"; let planets = planetStr.split(","); console.log(planets);

Output:
Mercury,Venus,Earth,Mars,Jupiter

Источник

Typescript работа со строками

Learn Latest Tutorials

Splunk tutorial

SPSS tutorial

Swagger tutorial

T-SQL tutorial

Tumblr tutorial

React tutorial

Regex tutorial

Reinforcement learning tutorial

R Programming tutorial

RxJS tutorial

React Native tutorial

Python Design Patterns

Python Pillow tutorial

Python Turtle tutorial

Keras tutorial

Preparation

Aptitude

Logical Reasoning

Verbal Ability

Company Interview Questions

Artificial Intelligence

AWS Tutorial

Selenium tutorial

Cloud Computing

Hadoop tutorial

ReactJS Tutorial

Data Science Tutorial

Angular 7 Tutorial

Blockchain Tutorial

Git Tutorial

Machine Learning Tutorial

DevOps Tutorial

B.Tech / MCA

DBMS tutorial

Data Structures tutorial

DAA tutorial

Operating System

Computer Network tutorial

Compiler Design tutorial

Computer Organization and Architecture

Discrete Mathematics Tutorial

Ethical Hacking

Computer Graphics Tutorial

Software Engineering

html tutorial

Cyber Security tutorial

Automata Tutorial

C Language tutorial

C++ tutorial

Java tutorial

.Net Framework tutorial

Python tutorial

List of Programs

Control Systems tutorial

Data Mining Tutorial

Data Warehouse Tutorial

Javatpoint Services

JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.

  • Website Designing
  • Website Development
  • Java Development
  • PHP Development
  • WordPress
  • Graphic Designing
  • Logo
  • Digital Marketing
  • On Page and Off Page SEO
  • PPC
  • Content Development
  • Corporate Training
  • Classroom and Online Training
  • Data Entry

Training For College Campus

JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week

Like/Subscribe us for latest updates or newsletter RSS Feed Subscribe to Get Email Alerts Facebook Page Twitter Page YouTube Blog Page

Источник

TypeScript — Strings

The String object lets you work with a series of characters. It wraps the string primitive data type with a number of helper methods.

Syntax

var var_name = new String(string);

A list of the methods available in String object along with their description is given below −

Returns a reference to the String function that created the object.

Returns the length of the string.

The prototype property allows you to add properties and methods to an object.

String Methods

A list of the methods available in String object along with their description is given below −

Читайте также:  Добавление html через css

Returns the character at the specified index.

Returns a number indicating the Unicode value of the character at the given index.

Combines the text of two strings and returns a new string.

Returns the index within the calling String object of the first occurrence of the specified value, or -1 if not found.

Returns the index within the calling String object of the last occurrence of the specified value, or -1 if not found.

Returns a number indicating whether a reference string comes before or after or is the same as the given string in sort order.

Used to match a regular expression against a string.

Used to find a match between a regular expression and a string, and to replace the matched substring with a new substring.

Executes the search for a match between a regular expression and a specified string.

Extracts a section of a string and returns a new string.

Splits a String object into an array of strings by separating the string into substrings.

Returns the characters in a string beginning at the specified location through the specified number of characters.

Returns the characters in a string between two indexes into the string.

The characters within a string are converted to lower case while respecting the current locale.

The characters within a string are converted to upper case while respecting the current locale.

Returns the calling string value converted to lower case.

Returns a string representing the specified object.

Returns the calling string value converted to uppercase.

Returns the primitive value of the specified object.

Источник

Оцените статью