Use the split() method to split a string by hyphen, e.g. str. split('-') . The split method takes a separator as a parameter and splits the string by the provided separator, returning an array of substrings.
Use the str. split() method to split a string by hyphen, e.g. my_list = my_str. split('-') .
Use the str. split() method to split a string on the colons, e.g. my_list = my_str. split(':') .
Use the String. split() method to split a string with multiple separators, e.g. str. split(/[-_]+/) . The split method can be passed a regular expression containing multiple characters to split the string with multiple separators.
a = '4-6'
b= '7:10'
I have already tried
a.split('-')
a.split(':')
how can i write code that can take in either string and get rid of both colons and hyphens? Is there a better way besides splitting the same string twice?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With