Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split a string based on either a colon or a hyphen? [duplicate]

Tags:

People also ask

How do you split a string with a hyphen?

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.

How do you split a string with a hyphen in Python?

Use the str. split() method to split a string by hyphen, e.g. my_list = my_str. split('-') .

How do you split a string with a colon in Python?

Use the str. split() method to split a string on the colons, e.g. my_list = my_str. split(':') .

How do you split a string with different separators?

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?