In JavaScript, we have a “string” dataType and it gives us some inbuilt functions now it gives us two functions one is the substring() and the second is the slice().
now the substring method is used to get part of a string. Think of it as a way to cut out a piece of the string. You tell the substring() method where to start and where to stop cutting the string.
for example:
let str = "hello world!" console.log(str.substring(0,2)) //output: "he"
now the slice method also works in the same way it takes the starting index and end index and returns the character between the given indexes
for example:
let str = "hello world!" console.log(str.slice(0,2)) //output: "he"
the output of the slice and substring method is the same so what is the main difference between them?
the main difference between the substring and slice methods is :
- Handling Negative Indices:
substring(): If you use negative values, they are treated as 0.
slice(): Negative values count from the end of the string.
let str = "Hello, World!"; console.log(str.substring(0,-1)) // Output: "" console.log(str.substring(-1,3)) // Output: "Hel" console.log(str.substring(1,-3)) // Output: "H" console.log(str.substring(0,-1)) // Output: "Hello, World"
2. Order of Parameters:
substring(): If the start index is greater than the end index, the substring will swap the values.
slice(): If the start index exceeds the end index, the slice will return an empty string.
let str = "Hello, World!"; console.log(str.substring(4,2)) // Output: "ll" console.log(str.slice(4,2)) // Output: ""
12 Comments
I pay a quick visit every day a few web sites and blogs
too read content, except this website offers quality based
articles. https://Www.waste-ndc.pro/community/profile/tressa79906983/
I like the comprehensive information you provide in your blog. The topic is kinda complex but I’d have to say you nailed it! Look into my page 57N for content about Airport Transfer.
I conceive this site has got some real excellent information for everyone. “The foundation of every state is the education of its youth.” by Diogenes.
Can you be more specific about the content of your article? After reading it, I still have some doubts. Hope you can help me.
Your writing style is cool and I have learned several just right stuff here. I can see how much effort you’ve poured in to come up with such informative posts. If you need more input about Cosmetics, feel free to check out my website at FQ7
Thanks for sharing. I read many of your blog posts, cool, your blog is very good.
Marcelino XQAwafGSBtzBSDsh 6 19 2022 priligy 30 mg B Body weights of mice under the conditions in A
Good content
Hello! I just would like to give a huge thumbs up for the great info you have here on this post. I will be coming back to your blog for more soon.
R can comprise a chemotherapeutic moiety; R can be selected from hydrogen, halogen, N R, N O R, N OR R, NO, NO, N R S O R, N R S O R, N R C O R, N R C O OR, N R C O N R, CN, COOR, CON R, OH, SH, C alkylsulfanyl, C alkylsulfinyl, C alkylsulfonyl, S O N R, S O N R, C fluoroalkyl, C fluoroalkoxy, C alkyl, substituted C alkyl, C alkoxy, substituted C alkoxy, C cycloalkyl, substituted C cycloalkyl, C cycloalkyloxy, substituted C cycloalkyloxy, C cycloalkylalkyl, substituted C cycloalkylalkyl, C aryl, substituted C aryl, C arylalkyl, substituted C arylalkyl, C heteroalkyl, substituted C heteroalkyl, C heteroalkoxy, substituted C heteroalkoxy, C heterocycloalkyl, substituted C heterocycloalkyl, C heterocycloalkylalkyl, substituted C heterocycloalkylalkyl, C heteroaryl, substituted C heteroaryl, C heteroarylalkyl, substituted C heteroarylalkyl, and a chemotherapeutic moiety; each of the other of R, R, and R can be independently selected from hydrogen, deuterio, halogen, OH, N R, NO, NO, CN, COOR, CON R, C alkylsulfanyl, C alkylsulfinyl, C alkylsulfonyl, C alkyl, substituted C alkyl, C cycloalkyl, substituted C cycloalkyl, C heteroalkyl, substituted C heteroalkyl, C alkoxy, substituted C alkoxy, C heteroalkoxy, substituted C heteroalkoxy, C cycloalkylalkyl, and C cycloalkylheteroalkyl; R can be selected from a carboxylic acid COOH, a carboxylic acid analog, a carboxylic acid bio isostere, hydroxamic acids CONR OH, boronic acids B OH OR, phosphinic acids or derivatives thereof PO OH R, and phosphonic acid or derivatives thereof PO OH OR, sulfinic acid SOOH, sulfonic acid SO OH, sulfonamide SO NHR or NHSO R, sulfonimide or acyl sulfonimide SO NHCOR or CONHSO R, sulfonylureas SO NHCONHR or NHCONHSO R, amide CONHR or NHCOR, acylcyanamide CONHCN, 2, 2, 2 trifluoroethan 1 ols CH CF OH, 2, 2, 2 trifluoromethyl ketones and hydrates thereof COCF and C OH CF, acidic heterocycles and annular tautomers of any of the foregoing, and acidic oxocarbocycles or cyclic polyones and resonance forms of any of the foregoing; wherein R is selected from hydrogen, C alkyl, C fluoroalkyl, C cycloalkyl, and C aryl; each R can be independently selected from hydrogen, deuterio, halogen, hydroxyl, C alkyl, C cycloalkyl, benzyl, and phenyl; or two R together with the carbon to which they are bonded form a ring selected from a C cycloalkyl ring and a C heterocycloalkyl ring; R can be selected from hydrogen, deuterio, C alkyl, substituted C alkyl, C heteroalkyl, substituted C heteroalkyl, C alkoxy, substituted C alkoxy, C heteroalkoxy, substituted C heteroalkoxy, C cycloalkyl, substituted C cycloalkyl, C cycloalkyloxy, substituted C cycloalkyloxy, OH, COOR, C fluoroalkyl, C fluoroalkoxy, C cycloalkyl, and phenyl; each R can be independently selected from hydrogen, C alkyl and C alkoxy, or two geminal R together with the nitrogen to which they are bonded form a 3 to 6 membered heterocyclic ring; L can be X в‚ђ, wherein, each X can be independently selected from a bond and C R, wherein each R is independently selected from hydrogen, deuterio, halogen, hydroxyl, C alkyl, and C alkoxy, or two R together with the carbon to which they are bonded form a C cycloalkyl ring or a C heterocycloalkyl ring, O, S, SO, SO, CO, and N R, wherein R is selected from hydrogen, and C alkyl; a can be selected from 0, 1, 2, 3, and 4; and where to buy generic cytotec tablets
Great write-up, I am normal visitor of one?¦s site, maintain up the nice operate, and It is going to be a regular visitor for a lengthy time.
Some genuinely excellent posts on this internet site, thankyou for contribution.