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.
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?
Examples of TypeScript string contains
Here are the following examples of TypeScript string contains:
let s1 = 'Sivaraman';
let subs2 = 'Welcome To My Domain have a nice day';
let vars = new Date(5, 2, 2021, 48, 30, 1000, 1300);
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());
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.
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";
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));
console.log("(11, 3): " + vars3.substr(11,3));
console.log("(18, 6): " + vars4.substr(18,6));
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.
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";
console.log("We have not found the mention string")
console.log("We have found the mention string")
console.log("Thanks for entering the inputs")
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.
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 –