To split a string into an array of words in TypeScript, you can use the split()
method of the String
object.
index.ts194 chars4 lines
Here, we first define a string sentence
that contains a sentence with multiple words separated by spaces. Then we use the split()
method to split this string into an array of words. The split()
method takes a delimiter string as its argument, which is used to split the original string into smaller strings wherever the delimiter occurs. In this case, we pass a space character as the delimiter string, so the original string is split into an array of words wherever there is a space character.
Finally, we log the resulting array of words to the console. This will output an array of strings, where each string in the array represents a single word from the original sentence.
gistlibby LogSnag