Updated April 5, 2023
Introduction to TypeScript substring
The Typescript substring is defined as one of the default methods in string classes. It will be used to return the subset of the string objects, and also, it will also be accepted for the other data types like integer the number ranges it starts from 0 and 1. It will be less than the length of the string characters it has followed the split() method. It is used to return the specific characters in the string mainly it focuses on the particular location through the specified number of characters with special characters with operators, length and starting position of the character is the parameters of the substr() method.
Syntax:
The TypeScript substring() method mainly focus on the string characters, and it will be used to specify the indexes, and it returns the new substrings characters in the TypeScript. It can be used to pass the two set of arguments one is starting the characters and the second parameter is the length of the string characters.
var variable name=""; // variable values should be of the string characters.
console.log("starting position of the character and end position of the character" + variable name.substr(starting position of the character and end position of the character));
----some typescript codes based on the user requirements-----
The above code is the basic syntax for using the substr() method in TypeScript based applications.
How does substring Work in TypeScript?
- The TypeScript string is one of the class for creating and utilising the string characters in the application. We use substring as one of the default method for split the string characters, and it will have concatenated using the length. This method will accept both numeric and non-numeric characters in the script. The string characters are starting with the beginning position of the specific location through the characters for each character; it will calculate the length.
- By using the length method, we can calculate the character’s length, and with the help of for loop, we can iterate the values and calculate the character’s length and validate the conditions based on their coder suggestions. Basically, the substring() method is pre-defined, and also it’s an inbuilt function for users to return the subset of the string objects. The argument and the parameters of the substring() method which represents the starting and ending indexes which can be used for to identify the characters and the substring() method which swaps the two set of arguments if the indexStart is greater than the indexEnd position and the string which is still returned position of the variables in the functions.
- When we use the string characters index with the help of the indexOf method, the substring method that is not to be used as the really purpose its main job is to return the indexes at the given substring is found on the TypeScript code.
Examples of TypeScript substring
Given below are the examples of TypeScript substring:
Example #1
Code:
var vars = "Welcome to My Domain Have a Nice Day iehf oiefb woeqiurfb iweofbc2iwe bic b newoinu";
var vars1 = "wugdv uidchh e237ed b 374trc hegdc jihefu jehdb jehfb jehb jhebi kbjeij heib jbie h";
var vars2 = "oehqf eub jbqe ijbj jb3e obe bou jbe joj3buier jorub jbj jboui e1r jobeur b ijoe iojef";
var vars3 = "dguy i eebc b eihyeuib f c uiycei ubc3 iurf eurv eub irfub o iobr ibr iu ior iou iub";
var vars4 = "oyed uery euir rui hu48 biury848 buy84 bui fj rhuj hrf hu iho4hi or4ui roh4ih rouiro h";
console.log("(2,3): " + vars.substr(2,3));
console.log("(-3,4): " + vars.substr(-3,4));
console.log("(3): " + vars1.substr(3));
console.log("(-16, 3): " + vars2.substr(-16,3));
console.log("(12, 3): " + vars2.substr(12,3));
console.log("(15, 5): " + vars3.substr(15,5));
console.log("(17, 12): " + vars4.substr(17,12));
Output:
In the above example, we used the substr() method in the different sequences. We used a numeric set of characters that is we can fetch and identified only the integer numbers that will be covered using both positive and negative numbers. With the help of the console.log() method, we can print the values in the output console by using the var keyword; we can declare the variable values by using the string quotations. Generally, the var datatype includes numeric and non-numeric characters here; we can cover the numeric value with the specified index positions for each step, and the variable values declared in the user input end.
Example #2
Code:
var vars = "Welcome to My Domain Have a Nice Day iehf okjwdkg kewgk jdgkiwye iugeigu buir br";
var vars1 = "wuhsgf jdghj 326875 38 3846 86347484 8634r 39846 468 48465 386 34798 94865 946";
var vars2 = "f wqd qw qwuef3287 j6487 jg857 kjg85 jy4587 ng875 gr8 jgweuf 478 jgeru 74";
var vars3 = " hjqwdf jhgds ieyw jhgu jhger 457 jgevr u5 jbi5t78 njerv 5u jngu5 biu5yn bnn uy5 r";
var vars4 = "wf jhqgwf37 jhg u4t jhuy bvu5y4 bv5n bv457b u45t7 bu54tg biu45y buy55y jhgu b ut bu";
var vars5 = new String( "Have a Nice Day" );
var ind = vars.indexOf( "jg jdsvh jsjv bj b kjbd sdbsksd bs dbs db bd" );
console.log("Your first output result is :" + ind );
var ind1 = vars1.indexOf( "ksadsfs jgkvc kgdv kjgdk kdsjv kjdg" );
console.log("Your second output result is:" + ind1 );
Output:
In the second example, we used indexOf() is one of the default child methods in the substring() method; with the help of this method, we can fetch and retrieve the indexing values and the position of the string characters. Additionally, we used var5 variable values; we can create the string object and store it as the separate memory address and its reference.
Example #3
Code:
var vars = "Welcome to My Domain Have a Nice Day iehf okjwdkg kewgk jdgkiwye iugeigu buir br";
var output = /My/gi;
if (vars.search(output) == -1 ) {
console.log("Sorry user it does not contain your values" );
} else {
console.log("Thanks user it contains your values" );
}
Output:
In the final example, we used the search() method for searching the characters, which is declared on the user input variable. We can also search and store the input characters using the other variable with the same data type(var). The search() method is one of the child method in the substring() function of the string classes.
Conclusion
The substring() method is one of the default and frequently used as the string classes in TypeScript. With the help of its child methods like search(), indexOf(), substr() methods, we can achieve this functionality like to calculate the starting and indexing position of the string characters and using loop/conditional statements for evaluating the values.
Recommended Articles
We hope that this EDUCBA information on “TypeScript substring” was beneficial to you. You can view EDUCBA’s recommended articles for more information.