TypeScript is the parent of JavaScript, and they both have similar methods. In fact, anywhere JavaScript runs, TypeScript runs as well. With the substring()
method, we can extract a certain part of a string by specifying where to start and end the extraction.
string.substring(start, end)
start
: This is an integer. It represents the index position in the string from where we want to start the extraction.
end
: This is also an integer that represents the end position where the extraction should stop.
A string is returned, which contains the extracted characters.
export {}// create some stringslet name:string = "Theodore"let role:string = "Software Developer"let hubby:string = "Coding, Writing, Reading"// extract parts of the stringslet extract1 = name.substring(0, 3)let extract2 = role.substring(9, 12)let extract3 = hubby.substring(8, 15)// print the extracted substringsconsole.log(extract1)console.log(extract2)console.log(extract3)
substrings
by setting the start and end index values. Then we save the results to some variables.substrings
to the console.