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 contains
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 contains

TypeScript string contains

Introduction to TypeScript string contains

In typescript, string contains is one of the features and also it is referred to and implemented using the includes() method which is used to determine the string characters whether it contains the characters of the specified string or not by using this method we can return the boolean values like true and false statements mainly this method includes() return the true value if the string values are satisfied the conditions else it returns the false also the includes() method is case-sensitive we also use other methods like search() method used for search the strings based on the requirement we can utilize the methods for to implement the application.

Syntax:

The string class and their default methods are used to access and implement the application based on the user inputs like string, integer formats. Among these string contains is one of the features and it can implement the user’s data as the simplest and fastest way to check and validate the string datas like substring or other logic we need to implement it by using the string default methods.

data type(var, local, const etc) variable name= string values;
conditional loops(variable name.includes(‘string values’))
{
---some typescript code logics depends on the requirement---
}

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

The above codes are the basic syntax for finding the logic for string contains with the help of the includes() method. Like that we can use other methods like search(), indexof(), etc. Based on the user requirement we can use the above methods.

How string contains work in Typescript?

Generally, the typescript and JavaScript contain many features and frameworks, other libraries to implement the application in more trends. The day-to-day \ updates are more when compared to the other script languages. Like that string is one of the most important features and it has many ways for to check the string contains like substring etc. It offers many ways but the most important and canonical way includes the () method this method will be used and supported for other modern browsers except internet explorer. Parallelly we use another method like indexof() is used to validate and check the string-like substring indexes and this method will always return the -1 if the string does not contain the substring on the user screen. If the substring is found, then it returns the index of the characters that can be the start and end of the specified strings. The includes() method always acts and calls the second parameter or arguments as the starting point of each sub-string character.   More ever other methods like find() these methods also achieved the same concept but different ways of the also if we use includes() method the string characters are validated only in the case-sensitive format other than that it can’t be validated.

Examples of TypeScript string contains

Here are the following examples of TypeScript string contains:

Example #1

let s1 = 'Sivaraman';
let subs2 = 'Welcome To My Domain have a nice day';
let vars = new Date(5, 2, 2021, 48, 30, 1000, 1300);
if(s1.indexOf(subs2))
{
vars.setDate(30);
vars.setMonth(12);
vars.setFullYear(2020);
vars.setHours(11);
vars.setMinutes(25);
vars.setSeconds(60);
console.log("Welcome To My domain = " + vars.getFullYear());
console.log("We can get the date by using this method  = " + vars.getDate());
console.log("We can get the month by using this method= " + vars.getMonth());
console.log("We can get the current day for the month and year= " + vars.getDay());
console.log("We can get the hours details = " + vars.getHours());
console.log("We can get the minutes details = " + vars.getMinutes());
console.log("We can get the seconds details = " + vars.getSeconds());
}

Output:

TypeScript string contains output 1

In the above example, we used the string contains method like indexof() by using this we can get the index of the specified string types. Additionally, we can calculate the current date, month, year, time, minutes, and seconds. With the help of default methods, the date and times are calculated according to that mentioned strings like user inputs. The s1.indexof(subs2) will be used for comparing the string characters in the index-based concepts.

Example #2

Code:

var vars = "Welcome To My Domain Have a Nice Day Users";
var vars1 = "My Second Input is Entered";
var vars2 = "My Third Input is Entered";
var vars3 = "My Fourth Input is Entered";
var vars4 = "My Fifth Input is Entered";
if(vars.search(vars1)== 1)
{
console.log("(7,1): "    + vars.substr(7,1));
console.log("(-1,-5): "   + vars.substr(-1,-5));
console.log("(4,1): "      + vars1.substr(4,1));
console.log("(-5, 7): " + vars2.substr(-5,7));
console.log("(9, 4): "  + vars2.substr(9,4));
}
else{
console.log("(11, 3): "  + vars3.substr(11,3));
console.log("(18, 6): "  + vars4.substr(18,6));
}

Output:

TypeScript string contains output 2

In the second example, we used substrings concepts for calculating the sub-strings in the given inputs. By using the search() method we can find the string characters and additionally substr() method for calling the proper index and position of the input strings. Same like the first example we used if conditions to evaluate the string contains method search() and if the condition is true it will execute and print the substring characters on the output console else it will exit on the loop and it execute the else loop condition.

Example #3

Code:

var first = "Welcome To My Domain Please enter your first input";
var second = "Welcome To My Domain Please enter your second input";
var third = "Welcome To My Domain Please enter your third input";
var four = "Welcome To My Domain Please enter your fourth input";
var five = "Welcome To My Domain Please enter your fifth input";
if(first.includes('the'))
{
console.log("We have not found the mention string")
}
else if(second.includes('My'))
{
console.log("We have found the mention string")
}
else
{
console.log("Thanks for entering the inputs")
}

Output:

output 3

In the final example, we used the includes() method to identify the strings on the mentioned variables. We used if, else if, and else statements for validating the string characters in the includes() method.

Conclusion

The typescript is the superscript of the javascript so more ever we have used the same methods and variables are used in the application. Like that string is the most important feature in the typescript it has some default methods like that we have calculate and search the strings using the include(), search(), and find() methods.

Recommended Articles

This is a guide to TypeScript string contains. Here we discuss How string contains work in Typescript and Examples along with the codes and outputs. You may also have a look at the following articles to learn more –

  1. Typescript Key-Value Pair
  2. TypeScript Set
  3. TypeScript Nullable
  4. TypeScript typeof
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