Updated August 4, 2023
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---
}
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:
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:
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:
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.