EDUCBA

EDUCBA

MENUMENU
  • Explore
    • Lifetime Membership
    • All in One Bundles
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Login
Home Software Development Software Development Tutorials TypeScript Tutorial TypeScript string contains

TypeScript string contains

Updated August 4, 2023

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.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

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

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

We hope that this EDUCBA information on “TypeScript string contains” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

  1. Typescript Key-Value Pair
  2. TypeScript Set
  3. TypeScript Nullable
  4. TypeScript typeof
ADVERTISEMENT
All in One Excel VBA Bundle
500+ Hours of HD Videos
15 Learning Paths
120+ Courses
Verifiable Certificate of Completion
Lifetime Access
ADVERTISEMENT
Financial Analyst Masters Training Program
2000+ Hours of HD Videos
43 Learning Paths
550+ Courses
Verifiable Certificate of Completion
Lifetime Access
ADVERTISEMENT
All in One Data Science Bundle
2000+ Hour of HD Videos
80 Learning Paths
400+ Courses
Verifiable Certificate of Completion
Lifetime Access
ADVERTISEMENT
All in One Software Development Bundle
5000+ Hours of HD Videos
149 Learning Paths
1050+ Courses
Verifiable Certificate of Completion
Lifetime Access
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • 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

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

EDUCBA

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

Let’s Get Started

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

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA

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

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

Forgot Password?

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

🚀 Extended Cyber Monday Price Drop! All in One Universal Bundle (3700+ Courses) @ 🎁 90% OFF - Ends in ENROLL NOW